Simple MEAN tutorial – App setup using ExpressJS part 2

Simple MEAN tutorial – part 1 RESTful Day


Starting up with package.json creation using

npm init

This will get you to options on setting up the project like below screenshot

at present we will give project name – example1

entry point file – server.js (or whatever it might be <project name>, <app.js>, etc.,) this file seems to be the entry point to our node application.

leave rest of the options blank for now.


npm-init setup package.json

Add Express JS dependency.

Next step is to add expressJS to our application as I said express is node-js framework to handle API calls and routing for our application.

command to add ExpressJS

npm install express –save

here express is installed to our project and it will update to our package.json due to the keyword –save

package.json model

package.json model

so if you open package.json and see it added as dependency like shown above in the image with corresponding module version(express with 4.0.0 version), so if you sharing your project to the team no need to share the dependency modules it will get installed by looking into package.json file or no problem getting conflict with different version, hence package.json will have the proper version for the app.

Create Entry point file “server.js”.

Now it is time to write our server startup code. Create server.js in your application folder and add following code.

var express  = require('express');
var app = express();

app.listen(3000, function(){
  console.log('server running on port 3000');

the above code does injects express module to the node server and creating express instance and storing it in app variable

and make the app instance on port 3000 using listen method and it has callback function which now we are used to write the console.log to show server is running…

then start the server using command

node server.js

now you can see the message “server running on port 3000″ in the console.

Code for REST calls.

As referred we have to create get, post, put and delete API calls for our application. First start with get method 

var express  = require('express');
var app = express();

app.get('/', function(req, res){
  res.send('Hello World');

app.listen(3000, function(){
  console.log('server running on port 3000');

from above code whenever the http://localhost:3000/ hits in browser it gets the ‘Hello World’ as output

first parameter in get method is API path ‘/’, it can be anything like ‘/products’ it is better to have a noun in rest api url example : products, books, movies, etc., and the verbs or action will be get, post, put and delete, request-method is used to identify which action it is performing

so for example if we change our get method API

app.get('/products', function(req, res){
  res.json({'prodId':'001', 'prodName':'blue casual shoe', 'prodDesc': 'desc of blue casual shoe'});

Whenever I hit http://localhost:3000/products it will give the json output

so we have created our first REST API get method…..

In the next blog we will connect with mongoDB and fetch some real data

    Leave a Reply

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

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>