- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Using post request in middleware in express
We used use() function to execute middleware’s . Example we used below will be executed for both http GET and POST method −
const http = require('http'); const express = require('express'); const bodyParser = require('body-parser'); const app = express(); app.use(bodyParser.urlencoded({extended: false})); app.use('/', (req, res,next)=>{ next(); }); app.use('/add-username', (req, res,next)=>{ res.send('<form action="/post-username" method="POST"> <input type="text" name="username"> <button type="submit"> Send </button> </form>'); }); app.use('/post-username', (req, res, next)=>{ console.log('data: ', req.body.username); res.redirect('/'); }); app.use('/', (req, res,next)=>{ res.send('<h1> first midleware: Hello Tutorials Point </h1>'); }); const server = http.createServer(app); server.listen(3000);
How to restrict middleware only for GET or only for POST http method etc.
We have http methods like get() and post() etc function similar to use() function.
app.post('/post-username', (req, res, next)=>{ console.log('data: ', req.body.username); res.redirect('/'); });
In above code snippet we used post() function. It will restrict a http request to only post types for url path ‘/post-username’
get() function −
app.get('/add-username', (req, res,next)=>{ res.send('<form action="/post-username" method="POST"> <input type="text" name="username"> <button type="submit"> Send</button> </form>'); });
Similar to get() and post(), we have delete(), patch, put() functions for respective http requests.
Complete App.js with get() and post() −
const http = require('http'); const express = require('express'); const bodyParser = require('body-parser'); const app = express(); app.use(bodyParser.urlencoded({extended: false})); app.use('/', (req, res,next)=>{ next(); }); app.get('/add-username', (req, res,next)=>{ res.send('<form action="/post-username" method="POST"> <input type="text" name="username"> <button type="submit"> Send </button> </form>'); }); app.post('/post-username', (req, res, next)=>{ console.log('data: ', req.body.username); res.redirect('/'); }); app.use('/', (req, res,next)=>{ res.send('<h1> first midleware: Hello Tutorials Point </h1>'); }); const server = http.createServer(app); server.listen(3000);
Execute : npm start
Browser output −
localhost:300/add-username
localhost:3000/post-username
- Related Articles
- Adding middleware in Express in Node.js
- Getting POST request IP address in Django
- How to create a POST request in Postman?
- How to make an HTTP POST request on iOS App using Swift?
- Skipping middleware in Express.js
- Using POST Methods in Perl
- What is middleware in Laravel?
- What is middleware in django
- Passing Information Using POST Method in Python
- How to read request parameters passed in URL using JSP?
- Creating Variables using Pre Request Script using Postman?
- How to handle errors in middleware C# Asp.net Core?
- How to send POST requests in JSON using HTTP Client in Android?
- Interrupt request register in 8259
- Making http request in React.js

Advertisements