What is REST?

REST stands for Representational State Transfer(sometimes spelled “ReST“.) It relies on a stateless, client-server, cacheable communications protocol — and in virtually all cases, the HTTP protocol is used. REST is an architecture style for designing networked applications.

There are six constraints of REST:

  • Uniform Interface
  • Stateless
  • Cacheable
  • Client-Server
  • Layered System
  • Code on Demand

RESTful applications use HTTP requests to perform CRUD operations. CRUD means C: Create, R: Read, U: Update and D: Delete.

In this tutorial, we will learn how to create a simple RESTful APIs using NodeJS + Expressjs + MongoDB

Tools needed:

Get started

Download and Install Node.js & MongoDB

Kindley run following commands to check NPM and MongoDB installed on your machine:

Create a Folder name restApiExample

Navigate to the root of your newly created folder

Now, init node project and Create a package.json file

Eventually, you will end up with something like this –
Create a file called server.js

In this file, we will be writing protocols to create our server.

Create a folder called api

Inside this folder name “api” we will have everything to create server i.e API Controller, Models, Routes, etc.

Now, create 3 mode folders named controllers, models, routes

Create usersController.js in the api/controllers folder, usersRoutes.js in the api/routes folder and userModel.js in the api/models folder –

Your folder structure should look like this now:

programmingschool.io/

Server setup

Let’s install express and nodemon, express will be used to create server while nodemon will help to keep track of changes to the application by watching changed files and automatically restart the server.

Now, Open the server.js file and type/copy the following code into it

https://www.programmingschool.io/On your terminal, run

output(in your terminal):

Setting up the Schema

Let’s install mongoose first –

Why Mongoose?

Mongoose is node package which is used to interact with MongoDB instance.

After installation, open the userModel.js file and type/copy following code into the file and save. Let’s define the schema.

Setting up the Routes

Routing refers to determining how an application will respond to a client request for a specific endpoint, which is HTTP request method(GET, PUT, POST, DELETE, etc…)

Each route is assigned to different route handler function, which is executed when the route is matched.

We require the controllers to handle function on each of the routes we have defined.

Define Routes-

Setting up the controller

Define all the function in usersController.js which are assigned to each of the usersRoutes.

Almost Done,

Open server.js file

  • connect with database i.e. MongoDB by adding a URL to the mongoose instance connection
  • install bodyParser to parse the API request data

Your updated server.js-

The final thing to do

Open your terminal and start MongoDB server

Once the MongoDB server is running, restart node server.

That’s all Folks CHEERS!!!.

Github link

6 COMMENTS

  1. Hello:
    I followed your instructions to the letter but when trying to run the example I get the following error:

    Error: Route.get() requires a callback function but got a [object Undefined]
    at Route.(anonymous function) [as get] (/home/jorge/restApiExample/node_modules/express/lib/router/route.js:202:15)
    at module.exports (/home/jorge/restApiExample/api/routes/usersRoutes.js:7:4)
    at Object. (/home/jorge/restApiExample/server.js:15:1)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)
    at bootstrap_node.js:609:3

    I am a total Node virgin. Could you please point the way to resolve this problem?

    Thanks much.
    Jorge

LEAVE A REPLY

Please enter your comment!
Please enter your name here