You are here

How to Create a Restful API Using Node.js and MongoDB

Since most developers know at least some JavaScript, Node.js is a convenient tool for introducing new projects. Node.js is a server-side platform built on Chrome’s JavaScript runtime for building scalable network applications. And since it runs on the V8 JavaScript Engine, it is incredibly fast.
 
It also offers additional benefits when using JavaScript on a Web server as well as a browser, since the two programming environments can communicate data structures in JSON that will work the same on both sides. In a recent tutorial on his AdrianMejia.com blog, Adrian Mejia shows followers how to create RESTful APIs using Node.js and MongoDB.
 
This tutorial is the second installment in a series of three. The first part discussed AngularJS for beginners, and this one starts by explaining what a REST API is, and how to install the MEAN stack backend of MongoDB, Express, AngularJS, and Node.js. Once followers have installed the MongoDB NPM (Node Package Manager) Mongoose, you have everything you need to build a simple API that allows users to CRUD Todo tasks from the database.
 
Once Mongo is up and running, followers are provided the code to create entries, followed by how to query that information, chain multiple queries using Mongo’s query language, update, and finally delete data.
 
Mr. Mejia then discusses the Web framework solution ExpressJS and provides some practical examples of the middlewares that process the requests to the server. Express 4.0 offers several default middlewares, but the author highlights several others as well.
 
The final sections here discuss connecting everything covered so far, creating the Todo model with Mongoose, building the API, and creating ExpressJS routes. All code for this project is provided, as is a link to the full repository on GitHub. The third and final installment in this series will explain how to connect AngularJS to this endpoint.

Be sure to read the next REST article: How to Access Any RESTful API Using the R Language

Original Article

Creating RESTful APIs with NodeJS and MongoDB Tutorial (Part II)

 

Comments