



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Express.js is a popular Node.js framework for building web applications, including single-page, multi-page, and hybrid apps. what Express.js is, how to install and use it, and provides an overview of routing and response methods. It also covers route parameters, route handlers, and the use of Express.Router for modular routing.
What you will learn
Typology: Slides
1 / 27
This page cannot be seen from the preview
Don't miss anything!
Dr. Adeel Ansari
npm install express
module."
anybody browses to the root of our web application which is http://localhost:.
The callback function will be used to send the string 'Hello World' to the web page.
The 'res' parameter is used to send content back to the web page. This 'res'
parameter is something that is provided by the 'request' module to enable one to
send content back to the web page.
client requests on port no 3000. You can specify any available port over here.
Routing determine the way in which an application responds to a client request to a particular
endpoint.
For example, a client can make a GET, POST, PUT or DELETE http request for various URL
such as the ones shown below;
In the above example,
If a GET request is made for the first URL, then the response should ideally be a list of books.
If the GET request is made for the second URL, then the response should ideally be a list of
Students.
So based on the URL which is accessed, a different functionality on the webserver will be
invoked, and accordingly, the response will be sent to the client. This is the concept of
routing.
http://localhost:3000/Books
http://localhost:3000/Students
app.METHOD(PATH, HANDLER)
var express = require('express');
var app = express();
app.route('/Node').get(function(req,res)
{
res.send("Tutorial on Node");
});
app.route('/Angular').get(function(req,res)
{
res.send("Tutorial on Angular");
});
app.get('/',function(req,res){
res.send('Welcome to Guru99 Tutorials');
}));
app.all('/secret', function (req, res, next) {
console.log('Accessing the secret section ...’)
next() // pass control to the next handler
})
Route path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/
req.params: { "userId": "34", "bookId": "8989" }
app.get('/users/:userId/books/:bookId', function (req, res) {
res.send(req.params)
app.get('/example/a', function (req, res) {
res.send('Hello from A!’)
})
app.get('/example/b', function (req, res, next) {
console.log('the response will be sent by the next function ...’)
next()
}, function (req, res) {
res.send('Hello from B!’)
})
var express = require('express');
var app = express();
var cb0 = function (req, res, next) {
console.log('CB0')
next()
var cb1 = function (req, res, next) {
console.log('CB1')
next()
var cb2 = function (req, res) {
res.send('Hello from C!')
app.get('/example/c', [cb0, cb1, cb2])
var server=app.listen( 3000 ,()=>{});