What is Express.
js
Express is a fast, assertive, essential and moderate web framework of Node.js.
You can assume express as a layer built on the top of the Node.js that helps manage a server
and routes.
It provides a robust set of features to develop web and mobile applications.
Features of Express framework:
It can be used to design single-page, multi-page and hybrid web applications.
It allows to setup middlewares to respond to HTTP Requests.
It defines a routing table which is used to perform different actions based on HTTP method
and URL.
It allows to dynamically render HTML Pages based on passing arguments to templates.
Why use Express?
Ultra fast I/O
Asynchronous and single threaded
MVC like structure
Robust API makes routing easy
Test Version of node & npm:-
>node --version
>npm –version
Node Package Manager(npm)
npm is the package manager for node. The npm Registry is a public collection of packages of
open-source code for Node.js, front-end web apps, mobile apps, robots, routers, and countless
other needs of the JavaScript community.
How to use npm?
There are two ways to install a package using npm: globally and locally.
Globally − This method is generally used to install development tools and CLI based packages.
To install a package globally, use the following code.
>npm install -g <package-name>
Locally − This method is generally used to install frameworks and libraries. A locally installed
package can be used only within the directory it is installed. To install a package locally, use
the same command as above without the -g flag.
npm install <package-name>
Whenever we create a project using npm, we need to provide a package.json file, which has
all the details about our project. npm makes it easy for us to set up this file.
Let us set up our development project.
Step 1 − Start your terminal/cmd, create a new folder named hello-world and cd (create
directory) into it –
D:\>md hello-world
D:\>cd hello-world
Step 2 − Now to create the package.json file using npm, use the following code.
>npm init
Output:-
D:\hello-world>npm init
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global`
instead.
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (hello-world)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to D:\hello-world\package.json:
"name": "hello-world",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
Is this OK? (yes) press y
D:\hello-world>
Step 3 − Now we have our package.json file set up, we will further install Express. To
install Express and add it to our package.json file, use the following command −
>npm install --save express
After install added some express dependency in package.json file
Goto node-models folder under project
D:\hello-world>cd node-models
D:\hello-world\node-models>dir
Tip − The --save flag can be replaced by the -S flag. This flag ensures that Express is
added as a dependency to our package.json file.
This has an advantage, the next time we need to install all the dependencies of our
project we can just run the command npm install and it will find the dependencies in this file
and install them for us.
To make our development process a lot easier, we will install a tool from npm,
nodemon. This tool restarts our server as soon as we make a change in any of our files,
otherwise we need to restart the server manually after each file modification.
> npm install -g nodemon
You can now start working on Express.
Create a new file called index.js and type the following in it.
Index.js
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send("I Love India!");
});
app.listen(3000);
>nodemon inde.js
Sometime nodemon not working
Try this admin console:-
Hello please follow these you can solve this,
-> open Admin PowerShell
type these commands.
1)Set-ExecutionPolicy RemoteSigned
2)Set-ExecutionPolicy Unrestricted
3)Get-ExecutionPolicy
4)Exit