ExpressJS is a web application framework that provides you with a simple API to build
websites, web apps and back ends. With ExpressJS, you need not worry about low level
protocols, processes, etc.
Express provides a minimal interface to build our applications. It provides us the tools that
are required to build our app. It is flexible as there are numerous modules available
on npm, which can be directly plugged into Express.
Express was developed by TJ Holowaychuk and is maintained by the Node.js foundation
and numerous open source contributors.
Why Express?
Unlike its competitors like Rails and Django, which have an opinionated way of building
applications, Express has no "best way" to do something. It is very flexible and pluggable.
Pug
Pug (earlier known as Jade) is a terse language for writing HTML templates. It -
MongoDB is an open-source, document database designed for ease of development and
scaling. This database is also used to store data.
Mongoose is a client API for node.js which makes it easy to access our database from our
Express application.
websites, web apps and back ends. With ExpressJS, you need not worry about low level
protocols, processes, etc.
1.What is Express?
Express provides a minimal interface to build our applications. It provides us the tools that
are required to build our app. It is flexible as there are numerous modules available
on npm, which can be directly plugged into Express.
Express was developed by TJ Holowaychuk and is maintained by the Node.js foundation
and numerous open source contributors.
Why Express?
Unlike its competitors like Rails and Django, which have an opinionated way of building
applications, Express has no "best way" to do something. It is very flexible and pluggable.
Pug
Pug (earlier known as Jade) is a terse language for writing HTML templates. It -
- Produces HTML
- Supports dynamic code
- Supports reusability (DRY)
MongoDB and Mongoose
MongoDB is an open-source, document database designed for ease of development and
scaling. This database is also used to store data.
Mongoose is a client API for node.js which makes it easy to access our database from our
Express application.
Express install
node --version
npm --version
npm --version
npm install -g <package-name>
➞Step 1: Start your terminal/cmd, create a new folder named hello-world and cd (create
directory) into it:
➟step 2
npm init
➞step 3
npm install --save express
ls node_modules #(dir node_modules for windows)
npm install -g nodemon
This is all we need to start development using the Express framework. 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.
We have set up the development, now it is time to start developing our first app using
Express. Create a new file called index.js and type the following in it.
Express. Create a new file called index.js and type the following in it.
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send("Hello world!");
});
app.listen(3000);
Save the file, go to your terminal and type the following.
nodemon index.js
This will start the server. To test this app,
ExpressJS – Routing
app.method(path, handler)
This METHOD can be applied to any one of the HTTP verbs – get, set, put, delete. An
alternate method also exists, which executes independent of the request type.
Path is the route at which the request will run.
This METHOD can be applied to any one of the HTTP verbs – get, set, put, delete. An
alternate method also exists, which executes independent of the request type.
Path is the route at which the request will run.
ExpressJS – HTTP Methods
➞GET ⇒The GET method requests a representation of the specified resource. Requests
using GET should only retrieve data and should have no other effect.
using GET should only retrieve data and should have no other effect.
➞POST ⇒The POST method requests that the server accept the data enclosed in the
request as a new object/entity of the resource identified by
request as a new object/entity of the resource identified by
➞PUT ⇒The PUT method requests that the server accept the data enclosed in the
request as a modification to existing object identified by the URI. If it does not
exist then the PUT method should create one.
request as a modification to existing object identified by the URI. If it does not
exist then the PUT method should create one.
➞DELETE ⇒The DELETE method requests that the server delete the specified resource.