In Express.js, we will learn how to start and use the Express Framework of node.js. To start with node.js, you should have the Node.js and the npm (node package manager) installed. If you don’t have these package, go to the Node setup to install node on your local system.

Topics Covered

  • Node Package Manager
  • Install Express
  • Install Global Package
  • Install Local Package
  • Uninstall Modules

By running the following commands in your terminal, confirm that node and npm are successfully installed on your system.

node --version
npm --version

You should get the similar output : 

C:\Users\Pankaj\Desktop\express demo>node --versionv 4.2.3
C:\Users\Pankaj\Desktop\express demo>npm --version 2.14.7

Now we have Node and npm setup installed, let us understand what npm is and how to use it.

Node Package Manager

Node Package Manager is a package manager that can be used for the javascript programming language and contains all the files you need for a module. It is the default package manager for the JavaScript runtime environment. Npm provides command line utility to install node.js Package, it can get easily installed on any Computer. There are also many packages which add commands for you to use in the command line. It can manage multiple versions of code and is used on the server side.

Install Express

Installing Express is very easy. Make sure that you’ve Node.js installed on your system as it won’t work without it. Then, you have to globally install the express framework to create a web application using Node terminal.

Install Global Package

This package is a command line tool. Use the following command to install express framework globally :

npm install -g express
C:\Users\Pankaj\Desktop\express demo>npm install -g express
express@4.16.2 C:\Users\Pankaj\AppData\Roaming\npm\node_modules\express
├── escape-html@1.0.3
├── array-flatten@1.1.1
├── setprototypeof@1.1.0
├── methods@1.1.2
├── utils-merge@1.0.1
├── cookie-signature@1.0.6
├── encodeurl@1.0.2
├── path-to-regexp@0.1.7
├── parseurl@1.3.2
├── fresh@0.5.2
├── range-parser@1.2.0
├── vary@1.1.2
├── content-disposition@0.5.2
├── serve-static@1.13.1
├── content-type@1.0.4
├── cookie@0.3.1
├── merge-descriptors@1.0.1
├── etag@1.8.1
├── safe-buffer@5.1.1
├── statuses@1.3.1
├── depd@1.1.2
├── qs@6.5.1
├── finalhandler@1.1.0 (unpipe@1.0.0)
├── on-finished@2.3.0 (ee-first@1.1.1)
├── debug@2.6.9 (ms@2.0.0)
├── proxy-addr@2.0.2 (forwarded@0.1.2, ipaddr.js@1.5.2)
├── accepts@1.3.4 (negotiator@0.6.1, mime-types@2.1.17)
├── type-is@1.6.15 (media-typer@0.3.0, mime-types@2.1.17)
├── send@0.16.1 (destroy@1.0.4, ms@2.0.0, mime@1.4.1, http-errors@1.6.2)
└── body-parser@1.18.2 (bytes@3.0.0, raw-body@2.3.2, iconv-lite@0.4.19, http-errors@1.6.2)
C:\Users\Pankaj\Desktop\express demo>

Install Local Package

This npm package is used to install libraries and frameworks. A local package can be used only within the directory it is installed. Use the same above command without the -g flag to install a package locally.

npm install express --save
C:\Users\Pankaj\Desktop\express demo>npm install express --save
express@4.16.2 node_modules\express
├── array-flatten@1.1.1
├── escape-html@1.0.3
├── utils-merge@1.0.1
├── merge-descriptors@1.0.1
├── setprototypeof@1.1.0
├── methods@1.1.2
├── cookie-signature@1.0.6
├── encodeurl@1.0.2
├── parseurl@1.3.2
├── content-type@1.0.4
├── cookie@0.3.1
├── fresh@0.5.2
├── path-to-regexp@0.1.7
├── serve-static@1.13.1
├── range-parser@1.2.0
├── vary@1.1.2
├── content-disposition@0.5.2
├── etag@1.8.1
├── safe-buffer@5.1.1
├── statuses@1.3.1
├── depd@1.1.2
├── qs@6.5.1
├── debug@2.6.9 (ms@2.0.0)
├── finalhandler@1.1.0 (unpipe@1.0.0)
├── type-is@1.6.15 (media-typer@0.3.0, mime-types@2.1.17)
├── accepts@1.3.4 (negotiator@0.6.1, mime-types@2.1.17)
├── proxy-addr@2.0.2 (forwarded@0.1.2, ipaddr.js@1.5.2)
├── on-finished@2.3.0 (ee-first@1.1.1)
├── body-parser@1.18.2 (bytes@3.0.0, raw-body@2.3.2, iconv-lite@0.4.19, http-errors@1.6.2)
└── send@0.16.1 (ms@2.0.0, destroy@1.0.4, mime@1.4.1, http-errors@1.6.2)

C:\Users\Pankaj\Desktop\express demo>

The above command installs and creates a directory named express inside the node_module. It also installs some other important modules along with express. The following list is given below –

body-parser :  

Body-parser is a node.js middleware for handling a JSON, Raw, Text, and URLs encoded form data.

The following command installs the body-parser :

npm install body-parser --save
C:\Users\Pankaj\Desktop\express demo>npm install body-parser --save
body-parser@1.18.2 node_modules\body-parser
├── content-type@1.0.4
├── bytes@3.0.0
├── depd@1.1.2
├── qs@6.5.1
├── iconv-lite@0.4.19
├── on-finished@2.3.0 (ee-first@1.1.1)
├── raw-body@2.3.2 (unpipe@1.0.0)
├── debug@2.6.9 (ms@2.0.0)
├── http-errors@1.6.2 (setprototypeof@1.0.3, inherits@2.0.3, statuses@1.4.0, depd@1.1.1)
└── type-is@1.6.15 (media-typer@0.3.0, mime-types@2.1.17)

C:\Users\Pankaj\Desktop\express demo>

cookie-parser :

Cookie-parser is used to parse header cookies and populate with req.cookies an object keyed by the cookie names.

The following command installs the cookie-parser :

npm install cookie-parser --save
C:\Users\Pankaj\Desktop\express demo>npm install cookie-parser --save
cookie-parser@1.4.3 node_modules\cookie-parser
├── cookie-signature@1.0.6
└── cookie@0.3.1
 
C:\Users\Pankaj\Desktop\express demo>

multer :

Multer is a node.js middleware for handling multipart/form-data, which is primarily used for uploading multiple files.

The following command installs theMulter :

npm install multer --save
C:\Users\Pankaj\Desktop\express demo>npm install multer --save
multer@1.3.0 node_modules\multer
├── object-assign@3.0.0
├── xtend@4.0.1
├── append-field@0.1.0
├── on-finished@2.3.0 (ee-first@1.1.1)
├── type-is@1.6.15 (media-typer@0.3.0, mime-types@2.1.17)
├── mkdirp@0.5.1 (minimist@0.0.8)
├── concat-stream@1.6.0 (inherits@2.0.3, typedarray@0.0.6, readable-stream@2.3.4)
└── busboy@0.2.14 (dicer@0.2.5, readable-stream@1.1.14)
 
C:\Users\Pankaj\Desktop\express demo>

 Example:

Let’s take a simple Express app example which starts a server and listen on a local port. It only responds to a homepage. For every other path, it will respond with a 404 Not Found error.

File: express_example.js  
var express = require('express');  
var app = express();  
app.get('/', function (req, res) {  
   res.send('Welcome to Pabbly.com');  
})  
var server = app.listen(8000, function () {  
var host = server.address().address  
  var port = server.address().port  
 console.log("Example app listening at http://%s:%s", host, port)  
})
Result:
C:\Users\Pankaj\Desktop\express demo>node exp.js
Example app listening at http://:::8000
Open http://127.0.0.1:8000/ in your browser to see the result.
Welcome to Pabbly.com

Uninstalling modules

To uninstall a module below is the following command :

npm uninstall express
C:\Users\Pankaj\Desktop\express demo>npm uninstall express
npm WARN expressfile@1.0.0 No description
npm WARN expressfile@1.0.0 No repository field.

removed 173 packages and updated 5 packages in 31.854s 

C:\Users\Pankaj\Desktop\express demo>

Learn More: