- 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
Express.js – app.post() Method
The app.post() method routes all the HTTP POST requests to the specified path with the specified callback functions.
Syntax
app.path(path, callback, [callback])
Parameters
- path − This is the path for which the middleware function is invoked. A path can be a string, path pattern, a regular expression or an array of all these.
- callback − These are the middleware functions or a series of middleware functions that acts like a middleware except that these callbacks can invoke next (route).
Example 1
Create a file with the name "appPost.js" and copy the following code snippet. After creating the file, use the command "node appPost.js" to run this code.
// app.post() Method Demo Example // Importing the express module const express = require('express'); // Initializing the express and port number var app = express(); // Initializing the router from express var router = express.Router(); var PORT = 3000; // Creating a POST request app.post('/api', (req, res) => { console.log("POST Request Called for /api endpoint") res.send("POST Request Called") }) // App listening on the below port app.listen(PORT, function(err){ if (err) console.log(err); console.log("Server listening on PORT", PORT); });
Hit the following Endpoint with a POST request
http://localhost:3000/api
Output
C:\home
ode>> node appPost.js Server listening on PORT 3000 POST Request Called for /api endpoint
Example 2
Let’s take a look at one more example.
// app.post() Method Demo Example // Importing the express module const express = require('express'); // Initializing the express and port number var app = express(); // Initializing the router from express var router = express.Router(); var PORT = 3000; // Creating a POST request app.post('/api', (req, res) => { console.log("Method called is -- ", req.method) res.end() }) // App listening on the below port app.listen(PORT, function(err){ if (err) console.log(err); console.log("Server listening on PORT", PORT); });
Now, hit the following Endpoint with a POST request
http://localhost:3000/api
Output
C:\home
ode>> node appPost.js Server listening on PORT 3000 Method called is -- POST
Advertisements