Professional Documents
Culture Documents
Express JS
Node.js is an amazing tool for building networking services and
applications.
Express is a Node.js Web Framework. ExpressJS is a web application
framework that provides you with a simple API to build websites, web
Express JS apps and back ends.
It is flexible as there are numerous modules available on npm, which
Dr.V.Mareeswari can be directly plugged into Express.
Assistant Professsor(Sr) Express was developed by TJ Holowaychuk and is maintained by
School of Information Technology and Engineering the Node.js foundation and numerous open source contributors.
VIT, Vellore
Cabin No:SJT 210-A30
C:\Users\admin\marees_node_express1.js Explanation
First, we import the express package to the express value.
const express = require('express') We instantiate an application by calling its app() method. app.get(route, callback)
const app = express() Once we have the application object, we tell it to listen for GET requests on the /path,
using the get() method.
app.get('/', (req, res) => res.send('Hello Mareeswari!')) There is a method for every HTTP verb: get(), post(), put(), delete(), patch().
app.listen(3000, () => console.log(‘Your Express Server is ready')) Those methods accept a callback function, which is called when a request is started, and we
need to handle it. An asynchronous function that is called when the server starts listening
for requests.
Express sends us two objects in this callback, which we called req and res, that represent
the Request and the Response objects.
Request is the HTTP request. It can give us all the info about that, including the request
parameters, the headers, the body of the request, and more. Response is the HTTP
response object that we’ll send to the client.
What we do in this callback is to send the ‘Hello World!’ string to the client, using
the Response.send() method. This method sets that string as the body, and it closes the
connection.
Dr.Mareeswari V/ AP(Sr) / SITE / VIT
The last line of the example actually starts the server, and tells it to listen on port 3000. We
Dr.Mareeswari V/ AP(Sr) / SITE / VIT
3 4
pass in a callback that is called when the server is ready to accept new requests.
In Visual Code
F:\Academic\Web Technologies\ITE1002\Programs\Node JS\index.js
let express = require('express')
let app = express()
app.get('/add', (req, res) =>
{
Run node index.js in
let n1=parseInt(req.query.num1);
Terminal
let n2=parseInt(req.query.num2); http://localhost:3000
let result=n1+n2; /add?num1=100&nu
res.send('Addition:'+result); m2=20 in chrome
});
app.listen(3000);
F:\Academic\Web Technologies\ITE1002\Programs\Node
JS\home.html
<html> <body> <h1>Hello</h1>
<form action="/name" method="GET">
<input type="text" name="t1">
<button>SEND</button> </form>
F:\Academic\Web Technologies\ITE1002\Programs\Node
JS\index.js
let express = require('express')
let server = express()
let path=require('path')
server.get('/home',(req,res)=>{
res.sendFile(path.join(__dirname,'home.html'))})
server.get('/name',(req,res)=>{
res.send('Hello '+req.query.t1)})
server.get('/add', (req, res) => {
let n1=parseInt(req.query.num1);
let n2=parseInt(req.query.num2);
let result=n1+n2;
res.send('Addition: '+result);});
server.listen(3000);
console.log("Server is ready"); Dr.Mareeswari V/ AP(Sr) / SITE / VIT 14 Dr.Mareeswari V/ AP(Sr) / SITE / VIT
13
To check if your cookie is set or not, just go to your browser, fire up C:\Users\admin\marees_node_cookie_expre
the console, and enter − ss.js
console.log(document.cookie);
var express = require('express')
You will get the output like (you may have more cookies set maybe
// npm install cookie-parser
due to extensions in your browser) −
var cookieParser = require('cookie-parser')
"name = marees"
var app = express();
The browser also sends back cookies every time it queries the server.
To view cookies from your server, on the server console in a route, // need cookieParser middleware before we can do anything with
add the following code to that route. cookies
console.log('Cookies:',req.cookies); app.use(cookieParser());
Next time you send a request to this route, you will receive the // set a cookie
following output. app.use(function (req, res, next) {
Cookies:{ name:'marees' } // check if client sent cookie
var cookie = req.cookies.cookieName; // RETRIEVE THE COOKIES
19 Dr.Mareeswari V/ AP(Sr) / SITE / VIT 20 Dr.Mareeswari V/ AP(Sr) / SITE / VIT
if (cookie === undefined) {// no: set a new cookie Install cookie-parser, running server and
var randomNumber=Math.random().toString(); running client
randomNumber=randomNumber.substring(2,randomNumber.length);
res.cookie('cookieName',randomNumber, { maxAge: 900000, httpOnly:
true });
console.log('cookie created successfully');
} else
{ // yes, cookie was already present
console.log('cookie exists', cookie);
}next(); }); // <-- important!
app.get('/', function(req, res) {
res.send(JSON.stringify(req.cookies));
}) app.listen(8081)
21 Dr.Mareeswari V/ AP(Sr) / SITE / VIT 22 Dr.Mareeswari V/ AP(Sr) / SITE / VIT