node js express post request code example
Example 1: express hello world
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
Example 2: express redirect
app.get('/', (req, res) => {
res.redirect('/foo/bar');
});
Example 3: express get url parameters
app.get('/path/:name', function(req, res) {
res.send("tagId is set to " + req.params.name);
});
Example 4: node express post request json
var express = require('express');
var app = express();
app.use(express.json());
app.post('/', function(request, response){
let myJson = request.body;
let myValue = request.body.myKey;
response.send(myJson);
});
app.listen(3000);
Example 5: app.post (req res) get data
const express=require('express');
const app=express();
app.post("/register/",function(req,res){
var bodyStr = '';
req.on("data",function(chunk){
bodyStr += chunk.toString();
});
req.on("end",function(){
res.send(bodyStr);
});
});