Part 1-Node.js Restful API Sample App

This is part 1 for the process to create a REST API using MEAN Stack. I am a learner and exploring different aspects of MEAN Stack. This series of writing is just to share with others what I am learning. Not been able to write every details of it but trying to share as much that might help to those who are new to MEAN Stack.

Step 1: Create Directory using command mkdir restapi

Step 2: Goto restapi directory: cd restapi

Step 3: Create package.json file #npm init

It will ask following information:

Application Name: restapi

Version: 1.0.0Description:

Entry point: By default it is index.js where as in this sample app I will use app.js

Test Command:

Git Repository:




Provide all the information as per requirements. However, you may leave something empty if not required.

Open the project in any editor. I prefer using Atom

Step 4: Open package.json file to add following dependencies:


We can write later about what each of the dependency does. Lets start with installing all the dependencies as per packag.json

Step 5: #npm installIt will take the contents of above created package.json file and will install all the dependencies in ‘node_modules’ within the project.

Step 6: Within the scripts section in package.json, add following:

“start” : “node app”

Command in command line “npm start” checks this “start” in the scripts section and takes app.js to start server.

Step 7: Create app.js file next to package.json. This is the file that we specified as entry point for the application.

Add following contents to app.js

const express = require(“express”);

const app = express();

const port = 4000;

app.listen(port, () => {

console.log(“Server is listening at port “+ port);


Step 8: Now, go to command line and put command:

#npm start

If you see a line in command-line saying “Server is listening at port 4000” It  means we are good to start our mini webserver.

However, in the browser http://localhost:4000 shows following output

Cannot Get /

This is because we have not yet defined any routes that we will do in next post but just to make a quick display

Step 9: Add following line before app.listen(****) function in app.js:

app.get(‘/’, (req, res) => {

res.send(“This is first to display”);


Here, we are accepting a get request by the without any Query String to display the message when user goes to home page at http://localhost:4000 after restarting server.

There is another option to not restart server at every step. However, I am not talking about details of that at this step.

For now, just press CTRL+C in command line to stop server and re-execute the command

#npm start

And visit http://localhost:4000 in your browser.

Leave a Reply

Your email address will not be published. Required fields are marked *