EXPRESS JS-ROUTING METHODS
Prepared By:
Mrs. Bareen Shaikh
Assistant Professor
Computer Science
MIT ACSC Alandi(D)
Pune
Basic Routing
▪ Routing refers to determining how an
application responds to a client request.
▪ Request is a URI path ,a specific HTTP request
method GET, POST, etc.
▪ Example
app.get('/', function (req, res) {
console.log("Got a GET request for the home page");
res.send('HelloGET');
})
Basic of routing
//This responds a GET request for the /list_user page.
app.get('/list_user', function (req, res) {
console.log("Got a GET request for /list_user");
res.send('Page Listing');
})
//This responds a GET request for abcd, abxcd, ab123cd,
and so on
app.get('/ab* cd', function(req, res) {
console.log("Got a GET request for /ab* cd");
res.send('Page Pattern Match');
})
Url building
▪ To use the dynamic routes, we should provide
different types of routes.
▪ Using dynamic routes allows us to pass parameters
and process based on them.
var express = require('express');
var app = express();
app.get('/:id', function(req, res){
res.send('The id you specified is ' + req.params.id);
});
app.listen(3000);
Give url as http://localhost:3000/4355
Url building
var express = require('express');
var app = express();
app.get('/parameter/:name/:id', function(req, res){
res.send('name is '+req.params.name +' id is ' + re
q.params.id);
});
app.listen(3000);
Give url as
http://localhost:3000/parameter/bareen/4355
Serving Static files
▪ Express provides a built-in middleware to serve
static files, such as images, css, JavaScript etc.
 express.static
▪ Pass the name of the directory,to the
express.static middleware to start serving the
files directly.
▪ For example, if images file in a directory
named public
▪ Path as public/images/mitlogo.png
▪ app.use(express.static('public'));
Serving Static files Example
var express = require('express');
var app = express();
app.use(express.static('public'));
app.get('/', function (req, res) {
res.send('HelloWorld');
});
var server = app.listen(3000, function () {
console.log(“Server running);
});
▪ node server.js
▪ open http://localhost:3000/images/mitlogo.png
Routing Methods
▪ GET requests are used to send only limited
amount of data because data is sent into
header.
▪ POST requests are used to send large
amount of data because data is sent in the
body.
▪ Express.js facilitates you to handle GET and
POST requests using the instance of express.
GET Method Example(get_info.html)
<html>
<body>
<form action="http://localhost:3000/process_get"
method="GET">
First Nam e: <input type="text" name="firstname">
<br>
Last Nam e: <input type="text" name="lastname">
<input type="submit" value="Submit">
</form >
</body>
</html>
GET Method Example(get_info.html)
var express = require('express');
var app = express();
app.use(express.static('public'));
app.get('/get_info.html ', function (req, res) {
res.send( __dirnam e + "/" + “get_info.html " );
})
app.get('/process_get', function (req, res) {
// Prepare output in JSON form at
response = {
first_nam e:req.query.first_name,
last_nam e:req.query.last_name
};
console.log(response);
res.end(JSON.stringify(response));
})
var server = app.listen(3000, function () {
console.log(“Server running”);
})
Post Method
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
// Create application/x-www-form -urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.use(express.static('public'));
app.get('/post_info.html', function (req, res) {
//res.send( __dirname + "/" + "post_info.html" );
})
app.post('/process_post', urlencodedParser, function (req, res) {
// Prepare output in JSON form at
response = {
first_name:req.body.fn,
last_name:req.body.ln
};
console.log(response);
res.end(JSON.stringify(response));
})
var server = app.listen(3000, function () {
console.log("Server running");
})

Express JS-Routingmethod.pdf

  • 1.
    EXPRESS JS-ROUTING METHODS PreparedBy: Mrs. Bareen Shaikh Assistant Professor Computer Science MIT ACSC Alandi(D) Pune
  • 2.
    Basic Routing ▪ Routingrefers to determining how an application responds to a client request. ▪ Request is a URI path ,a specific HTTP request method GET, POST, etc. ▪ Example app.get('/', function (req, res) { console.log("Got a GET request for the home page"); res.send('HelloGET'); })
  • 3.
    Basic of routing //Thisresponds a GET request for the /list_user page. app.get('/list_user', function (req, res) { console.log("Got a GET request for /list_user"); res.send('Page Listing'); }) //This responds a GET request for abcd, abxcd, ab123cd, and so on app.get('/ab* cd', function(req, res) { console.log("Got a GET request for /ab* cd"); res.send('Page Pattern Match'); })
  • 4.
    Url building ▪ Touse the dynamic routes, we should provide different types of routes. ▪ Using dynamic routes allows us to pass parameters and process based on them. var express = require('express'); var app = express(); app.get('/:id', function(req, res){ res.send('The id you specified is ' + req.params.id); }); app.listen(3000); Give url as http://localhost:3000/4355
  • 5.
    Url building var express= require('express'); var app = express(); app.get('/parameter/:name/:id', function(req, res){ res.send('name is '+req.params.name +' id is ' + re q.params.id); }); app.listen(3000); Give url as http://localhost:3000/parameter/bareen/4355
  • 6.
    Serving Static files ▪Express provides a built-in middleware to serve static files, such as images, css, JavaScript etc.  express.static ▪ Pass the name of the directory,to the express.static middleware to start serving the files directly. ▪ For example, if images file in a directory named public ▪ Path as public/images/mitlogo.png ▪ app.use(express.static('public'));
  • 7.
    Serving Static filesExample var express = require('express'); var app = express(); app.use(express.static('public')); app.get('/', function (req, res) { res.send('HelloWorld'); }); var server = app.listen(3000, function () { console.log(“Server running); }); ▪ node server.js ▪ open http://localhost:3000/images/mitlogo.png
  • 8.
    Routing Methods ▪ GETrequests are used to send only limited amount of data because data is sent into header. ▪ POST requests are used to send large amount of data because data is sent in the body. ▪ Express.js facilitates you to handle GET and POST requests using the instance of express.
  • 9.
    GET Method Example(get_info.html) <html> <body> <formaction="http://localhost:3000/process_get" method="GET"> First Nam e: <input type="text" name="firstname"> <br> Last Nam e: <input type="text" name="lastname"> <input type="submit" value="Submit"> </form > </body> </html>
  • 10.
    GET Method Example(get_info.html) varexpress = require('express'); var app = express(); app.use(express.static('public')); app.get('/get_info.html ', function (req, res) { res.send( __dirnam e + "/" + “get_info.html " ); }) app.get('/process_get', function (req, res) { // Prepare output in JSON form at response = { first_nam e:req.query.first_name, last_nam e:req.query.last_name }; console.log(response); res.end(JSON.stringify(response)); }) var server = app.listen(3000, function () { console.log(“Server running”); })
  • 11.
    Post Method var express= require('express'); var app = express(); var bodyParser = require('body-parser'); // Create application/x-www-form -urlencoded parser var urlencodedParser = bodyParser.urlencoded({ extended: false }) app.use(express.static('public')); app.get('/post_info.html', function (req, res) { //res.send( __dirname + "/" + "post_info.html" ); }) app.post('/process_post', urlencodedParser, function (req, res) { // Prepare output in JSON form at response = { first_name:req.body.fn, last_name:req.body.ln }; console.log(response); res.end(JSON.stringify(response)); }) var server = app.listen(3000, function () { console.log("Server running"); })