1//make sure it is in this order
2npm i body-parser
3
4const express = require('express')
5const bodyParser = require('body-parser')
6
7const 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')
18 res.end(JSON.stringify(req.body, null, 2))
19})
20
1// Express v4.16.0 and higher
2// --------------------------
3const express = require('express');
4
5app.use(express.json());
6app.use(express.urlencoded({
7 extended: true
8}));
9
10// For Express version less than 4.16.0
11// ------------------------------------
12const bodyParser = require('body-parser');
13
14app.use(bodyParser.json());
15app.use(bodyParser.urlencoded({
16 extended: true
17}));
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">
1/** @format */
2
3const express = require("express");
4const app = express();
5const mongoose = require("mongoose");
6const bodyParser = require("body-parser");
7const PORT = process.env.PORT || 3000;
8
9// parse application/x-www-form-urlencoded
10app.use(bodyParser.urlencoded({ extended: false }));
11
12// parse application/json
13app.use(bodyParser.json());
14
15//connecting to db
16try {
17 mongoose.connect('mongodb://localhost/YOUR_DB_NAME', {
18 useNewUrlParser: true,
19 useUnifiedTopology: true,
20 useCreateIndex: true,
21 }, () =>
22 console.log("connected"));
23 } catch (error) {
24 console.log("could not connect");
25 }
26
27app.get("/", (req, res) => {
28 res.send("home");
29});
30
31app.listen(PORT, () => console.log(`Server is listening on port ${PORT}`));
32
1const express = require('express')
2const bodyParser = require('body-parser')
3
4const app = express()
5
6// parse application/x-www-form-urlencoded
7app.use(bodyParser.urlencoded({ extended: false }))
8
9// parse application/json
10app.use(bodyParser.json())
11
12app.use(function (req, res) {
13 res.setHeader('Content-Type', 'text/plain')
14 res.write('you posted:\n')
15 res.end(JSON.stringify(req.body, null, 2))
16})
17
18