0% found this document useful (0 votes)
7 views

Express

Express concept and how to use

Uploaded by

Aman Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
7 views

Express

Express concept and how to use

Uploaded by

Aman Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 10
Saleen te et ME With concrete examples... INSTALLATION Install Express globally or in your project. # Install Express npm install express --save Con aon a oe const app = express(); // Create an Express application Cora aera ns app.get('/', (req, res) => { res.send( ‘Hello, World!'); // Respond with ‘Hello, World!" Ey) amet 1a eae Co Cee ice CM Temas ears iad ae} * express() creates an instance of an Express application. * app.get() handles HTTP GET requests. * app.listen() starts the server on the specified port. Middleware functions are functions that have access to the request (req), response (res), and next middleware in the request-response cycle. Cort Taio Paes oe tot CCC ir Cec aC oa aes tcc eae) fad eee LO ae ETO et Mi ceCeLN ue Led For serving static files like images, CSS, etc. app.use(express.static( 'public')); eT Sa Me Ce Laie Lay To parse incoming JSON requests. app.use(express. json()); Routing in Express is defining how the application responds to different HTTP methods and paths. Basic Routing app.get('/', (req, res) => { res.send('GET request received'); a app.post('/submit', (req, res) => { res.send('POST request received'); a Route and Query Parameters app.get('/user/:id', (req, res) => { const userId = req.params.id; // Route Parameter Cool ETL 01 een Le LL res.send(“User ID: ${userId}, Search Query: ${searchQuery || 'No query provided'}*); ca REQUEST AND RESPONSE OBJECTS Request Object (req) ¢ req.body: Contains parsed data from POST ite [Ue * req.params: Access route parameters like /user/:id. * req.query: Access query string parameters like /search?q=term. Response Object (res) o Ae Tb Sends a response (text or data). * res.json(): Sends a JSON response. * res.status(): Sets the HTTP status code. * res.redirect(): Redirects the request to another 10) app.get('/user/:id', (req, res) => { res.status(200).json({ userId: req.params.id }); Basic Error Handling app.use((err, req, res, next) => { console.error(err.stack) ; res.status(500).send( 'Something broke! '); ak Use next(err) to pass errors to the middleware chain. Xelet a Cold me (oh c] npm install body-parser --save const bodyParser = require( 'body-parser' ); app.use(bodyParser.urlencoded({ extended: true })); Handling form submi: app.post('/form-submit', (req, res) => { const username = req.body.username; ore gD Lee MAG YL aS OTe 9 TEMPLATE ENGINES Using EJS (Embedded JavaScript) npm install ejs --save app.set( ‘view engine’, ‘ejs'); // Set the view engine to ejs app.get('/user/:name', (req, res) => { Coe ee kee eC Oe eC) ¢ Views folder: By default, Express looks for templates in the views folder. * Passing data: { username: req.params.name } passes data to the template. Example EJS template (views/profile.ejs): SN ead ele LT SAD ed Modularize your routes by using the Router. Creating a Router: const express = require('express' const router = express.Router(); router.get('/', (req, res) => { res.send( ‘Welcome to the Home Page’); router.get('/about', (req, res) => { res.send( ‘About Page’ ); A module.exports = router; Using Router in app.js: const homeRouter = require('./routes/home' ) ; app.use('/', homeRouter); // Mount the route Install the multer middleware to handle file uploads. npm install multer --save File upload setup: onsae ee mee eT Tet ta) Cea CCR iG idem Coen CCT VAR O app.post('/upload', upload.single('file'), (req, res) => { eee aS ele cesar nS a

You might also like