2. 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');
})
3. 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');
})
4. 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
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 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
8. 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.
9. 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>
10. 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”);
})
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");
})