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
[email protected] C:\Users\Pankaj\AppData\Roaming\npm\node_modules\express
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected])
└── [email protected] ([email protected], [email protected], [email protected], [email protected])
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
[email protected] node_modules\express
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected])
└── [email protected] ([email protected], [email protected], [email protected], [email protected])
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
[email protected] node_modules\body-parser
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected])
└── [email protected] ([email protected], [email protected])
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
[email protected] node_modules\cookie-parser
├── [email protected]
└── [email protected]
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
[email protected] node_modules\multer
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected], [email protected])
└── [email protected] ([email protected], [email protected])
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 [email protected] No description
npm WARN [email protected] No repository field.
removed 173 packages and updated 5 packages in 31.854s
C:\Users\Pankaj\Desktop\express demo>
Learn More: