Routing
mkdir my-express-app
cd my-express-app
npm init
npm install express --save
The following code is an example of a very basic
route.
const express = require('express')
const app = express()
// respond with "hello world"
when a GET request is made to the
homepage
app.get('/', (req, res) => {
res.send('hello world')
})
const express = require('express');
const app = express();
app.use((req, res) => {
res.send('<h1>Hello, World!</h1>');
});
app.get(‘/’,(req, res) => {
res.send('<h1>Hello, World!</h1>');
});
app.get(‘/use’,(req, res) => {
res.send('<h1>Hello, About US!</h1>');
});
app.listen(8000, () => console.log('Server listening on
port 8000'));
The following code is an example of a very basic
route.
const express = require('express')
const app = express()
// respond with "hello world"
when a GET request is made to the
homepage
app.get('/', (req, res) => {
res.send('hello world')
})
const express = require('express');
const app = express();
app.use((req, res) => {
res.send('<h1>Hello, World!</h1>');
});
app.get(‘/’,(req, res) => {
res.send('<h1>Hello, World!</h1>');
});
app.get(‘/use’,(req, res) => {
res.send('<h1>Hello, World!</h1>');
});
app.listen(8000, () => console.log('Server listening on
port 8000'));
 npm install -g nodemon
 npm install --save-dev nodemon
 nodemon app.js
// Importing the module
const express=require("express")
// Creating express Router
const router=express.Router()
// Handling login request
router.get("/login",(req,res,next)=>{
res.send("This is the login request")
})
module.exports=router
// Importing express module
const express=require("express")
const router=express.Router()
// Handling request using router
router.get("/home",(req,res,next)=>{
res.send("This is the homepage request")
})
// Exporting the router
module.exports=router
const express=require("express")
// Importing all the routes
const homeroute=require("./routes/Home.js")
const loginroute=require("./routes/login.js")
// Creating express server
const app=express()
// Handling routes request
app.use("/",homeroute)
app.use("/",loginroute)
app.listen((3000),()=>{
console.log("Server is Running")
})
const express = require("express");
const app = express();
app.get("/", function(req, res) {
res.send("This is a get request!!n");
});
app.post("/", function(req, res) {
res.send("This is a post request!!n");
});
app.put("/", function(req, res) {
res.send("This is a put request!!n");
});
app.get("/hey", function(req, res) {
res.send("This is a get request to '/hey'!!n");
});
app.listen(3000);
const express = require('express');
const app = express();
// Define a route with a route parameter
app.get('/users/:userId', (req, res) => {
// Access the route parameter value
const userId = req.params.userId;
res.send(`User ID: ${userId}`);
});
// Start the server
app.listen(3000, () => {
console.log('The server is running on port: 3000');
});
const express = require('express');
const app = express();
// Custom middleware to log requests
function logRequests(req, res, next) {
console.log(`${req.method} request to ${req.url} at $
{new Date().toLocaleTimeString()}`);
next(); // Proceed to the next middleware or route
}
app.use(logRequests); // Apply it globally
app.get('/', (req, res) => {
res.send('Home Page');
});
app.get('/about', (req, res) => {
res.send('About Page');
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
MULTIPLE MIDDLE
function first(req, res, next) {
console.log('First middleware');
next(); // Move to the next middleware
}
function second(req, res, next) {
console.log('Second middleware');
next(); // Move to the next middleware
}
app.get('/multi', first, second, (req, res) => {
res.send('Multiple middleware executed');
});
Cookies
var express = require('express’)
var cookieParser = require('cookie-parser’)
var app = express() app.use(cookieParser())
app.get('/', function (req, res)
{ // Cookies that have not been signed
console.log('Cookies: ', req.cookies)
// Cookies that have been signed
console.log('Signed Cookies: ', req.signedCookies) })
app.listen(8080)
// curl command that sends an HTTP request with two cookies
// curl http://127.0.0.1:8080 --cookie "Cho=Kim;Greet=Hello"
https://www.npmjs.com/package/cookie-session
Simple view counter example
var cookieSession = require('cookie-session’)
var express = require('express’)
var app = express() app.set('trust proxy', 1)
// trust first proxy
app.use(cookieSession(
{ name: 'session', keys: ['key1', 'key2'] }))
app.get('/', function (req, res, next) {
// Update views
req.session.views = (req.session.views || 0) + 1
// Write response
res.end(req.session.views + ' views') })
app.listen(3000)

More Related Content

PPTX
Express JS
PPT
Building your first Node app with Connect & Express
PPTX
Building Web Apps with Express
PPTX
node.js workshop- node.js middleware
PDF
node.js practical guide to serverside javascript
PPTX
Switch to Backend 2023 | Day 1 Part 2
PDF
Node.js server-side rendering
PDF
Express JS-Routingmethod.pdf
Express JS
Building your first Node app with Connect & Express
Building Web Apps with Express
node.js workshop- node.js middleware
node.js practical guide to serverside javascript
Switch to Backend 2023 | Day 1 Part 2
Node.js server-side rendering
Express JS-Routingmethod.pdf

Similar to express-express-express-express-express- (20)

PPTX
Node.js Express
KEY
Writing robust Node.js applications
PDF
Build Web Apps using Node.js
PDF
Future Decoded - Node.js per sviluppatori .NET
PDF
Using Sinatra to Build REST APIs in Ruby
PPTX
Managing State in React Apps with RxJS by James Wright at FrontCon 2019
PDF
Silex Cheat Sheet
PDF
Silex Cheat Sheet
PPT
Going crazy with Node.JS and CakePHP
PPTX
Node.js System: The Approach
PDF
Node.jsやってみた
PDF
express of full stack web development notes
PPTX
Fast is Best. Using .NET MinimalAPIs
PPT
Async programming on NET
PDF
Workshop 4: NodeJS. Express Framework & MongoDB.
KEY
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
PDF
Build web application by express
PDF
Reduxing like a pro
PDF
Make WordPress realtime.
PDF
I Phone On Rails
Node.js Express
Writing robust Node.js applications
Build Web Apps using Node.js
Future Decoded - Node.js per sviluppatori .NET
Using Sinatra to Build REST APIs in Ruby
Managing State in React Apps with RxJS by James Wright at FrontCon 2019
Silex Cheat Sheet
Silex Cheat Sheet
Going crazy with Node.JS and CakePHP
Node.js System: The Approach
Node.jsやってみた
express of full stack web development notes
Fast is Best. Using .NET MinimalAPIs
Async programming on NET
Workshop 4: NodeJS. Express Framework & MongoDB.
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Build web application by express
Reduxing like a pro
Make WordPress realtime.
I Phone On Rails
Ad

Recently uploaded (20)

PDF
Weekly quiz Compilation Jan -July 25.pdf
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PDF
Trump Administration's workforce development strategy
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PDF
HVAC Specification 2024 according to central public works department
PDF
Hazard Identification & Risk Assessment .pdf
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
Weekly quiz Compilation Jan -July 25.pdf
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
TNA_Presentation-1-Final(SAVE)) (1).pptx
Environmental Education MCQ BD2EE - Share Source.pdf
B.Sc. DS Unit 2 Software Engineering.pptx
Trump Administration's workforce development strategy
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
HVAC Specification 2024 according to central public works department
Hazard Identification & Risk Assessment .pdf
A powerpoint presentation on the Revised K-10 Science Shaping Paper
Uderstanding digital marketing and marketing stratergie for engaging the digi...
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
Cambridge-Practice-Tests-for-IELTS-12.docx
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
LDMMIA Reiki Yoga Finals Review Spring Summer
Ad

express-express-express-express-express-

  • 2. mkdir my-express-app cd my-express-app npm init npm install express --save
  • 3. The following code is an example of a very basic route. const express = require('express') const app = express() // respond with "hello world" when a GET request is made to the homepage app.get('/', (req, res) => { res.send('hello world') }) const express = require('express'); const app = express(); app.use((req, res) => { res.send('<h1>Hello, World!</h1>'); }); app.get(‘/’,(req, res) => { res.send('<h1>Hello, World!</h1>'); }); app.get(‘/use’,(req, res) => { res.send('<h1>Hello, About US!</h1>'); }); app.listen(8000, () => console.log('Server listening on port 8000'));
  • 4. The following code is an example of a very basic route. const express = require('express') const app = express() // respond with "hello world" when a GET request is made to the homepage app.get('/', (req, res) => { res.send('hello world') }) const express = require('express'); const app = express(); app.use((req, res) => { res.send('<h1>Hello, World!</h1>'); }); app.get(‘/’,(req, res) => { res.send('<h1>Hello, World!</h1>'); }); app.get(‘/use’,(req, res) => { res.send('<h1>Hello, World!</h1>'); }); app.listen(8000, () => console.log('Server listening on port 8000'));
  • 5.  npm install -g nodemon  npm install --save-dev nodemon  nodemon app.js
  • 6. // Importing the module const express=require("express") // Creating express Router const router=express.Router() // Handling login request router.get("/login",(req,res,next)=>{ res.send("This is the login request") }) module.exports=router // Importing express module const express=require("express") const router=express.Router() // Handling request using router router.get("/home",(req,res,next)=>{ res.send("This is the homepage request") }) // Exporting the router module.exports=router
  • 7. const express=require("express") // Importing all the routes const homeroute=require("./routes/Home.js") const loginroute=require("./routes/login.js") // Creating express server const app=express() // Handling routes request app.use("/",homeroute) app.use("/",loginroute) app.listen((3000),()=>{ console.log("Server is Running") })
  • 8. const express = require("express"); const app = express(); app.get("/", function(req, res) { res.send("This is a get request!!n"); }); app.post("/", function(req, res) { res.send("This is a post request!!n"); }); app.put("/", function(req, res) { res.send("This is a put request!!n"); }); app.get("/hey", function(req, res) { res.send("This is a get request to '/hey'!!n"); }); app.listen(3000);
  • 9. const express = require('express'); const app = express(); // Define a route with a route parameter app.get('/users/:userId', (req, res) => { // Access the route parameter value const userId = req.params.userId; res.send(`User ID: ${userId}`); }); // Start the server app.listen(3000, () => { console.log('The server is running on port: 3000'); });
  • 10. const express = require('express'); const app = express(); // Custom middleware to log requests function logRequests(req, res, next) { console.log(`${req.method} request to ${req.url} at $ {new Date().toLocaleTimeString()}`); next(); // Proceed to the next middleware or route } app.use(logRequests); // Apply it globally app.get('/', (req, res) => { res.send('Home Page'); }); app.get('/about', (req, res) => { res.send('About Page'); }); app.listen(3000, () => { console.log('Server running on port 3000'); });
  • 11. MULTIPLE MIDDLE function first(req, res, next) { console.log('First middleware'); next(); // Move to the next middleware } function second(req, res, next) { console.log('Second middleware'); next(); // Move to the next middleware } app.get('/multi', first, second, (req, res) => { res.send('Multiple middleware executed'); });
  • 13. var express = require('express’) var cookieParser = require('cookie-parser’) var app = express() app.use(cookieParser()) app.get('/', function (req, res) { // Cookies that have not been signed console.log('Cookies: ', req.cookies) // Cookies that have been signed console.log('Signed Cookies: ', req.signedCookies) }) app.listen(8080) // curl command that sends an HTTP request with two cookies // curl http://127.0.0.1:8080 --cookie "Cho=Kim;Greet=Hello" https://www.npmjs.com/package/cookie-session
  • 14. Simple view counter example var cookieSession = require('cookie-session’) var express = require('express’) var app = express() app.set('trust proxy', 1) // trust first proxy app.use(cookieSession( { name: 'session', keys: ['key1', 'key2'] })) app.get('/', function (req, res, next) { // Update views req.session.views = (req.session.views || 0) + 1 // Write response res.end(req.session.views + ' views') }) app.listen(3000)