1// Express/Connect top-level generic
2// This example demonstrates adding a generic JSON and URL-encoded parser as a top-level middleware, which will parse the bodies of all incoming requests.
3// This is the simplest setup.
4
5var express = require('express')
6var bodyParser = require('body-parser')
7var app = express()
8
9// parse application/x-www-form-urlencoded
10app.use(bodyParser.urlencoded({ extended: false }))
11
12// parse application/json
13app.use(bodyParser.json())
14
15app.use(function (req, res) {
16 res.setHeader('Content-Type', 'text/plain')
17 res.write('you posted:\n')
18res.end(JSON.stringify(req.body, null, 2))})
1var express = require('express')
2 var bodyParser = require('body-parser')
3 var app = express()
4 // parseapplication/x-www-form-urlencoded
5 app.use(bodyParser.urlencoded({ extended: false }))
6 // parse application/json
7 app.use(bodyParser.json()) app.use(function (req, res) { res.setHeader('Content-Type', 'text/plain') res.write('you posted:\n') res.end(JSON.stringify(req.body, null, 2))})
1<script>
2const bodyParser = require("body-parser");
3
4app.use(bodyParser.urlencoded({extended:true}));
5
6app.post("/", function(req, res){
7 let firstName = req.body.fNAME;
8
9});
10</script>
11
12<input type="text" name="fNAME" placeholder="First Name">
1var express = require('express')
2var bodyParser = require('body-parser')
3
4var app = express()
5
6// create application/json parser
7var jsonParser = bodyParser.json()
8
9// create application/x-www-form-urlencoded parser
10var urlencodedParser = bodyParser.urlencoded({ extended: false })
11
12// POST /login gets urlencoded bodies
13app.post('/login', urlencodedParser, function (req, res) {
14 res.send('welcome, ' + req.body.username)
15})
16
17// POST /api/users gets JSON bodies
18app.post('/api/users', jsonParser, function (req, res) {
19 // create user in req.body
20})