express body-parser deprecated
The package bodyParser is deprecated. You will get this warning with these lines of code:
app.use(bodyparser.json());
app.use(bodyParser.urlencoded({extended: true}));
If you are using Express 4.16+ you can now replace those lines with:
app.use(express.json());
app.use(express.urlencoded()); //Parse URL-encoded bodies
3.5
2
app.use(bodyParser.urlencoded({ extended: true }))
Thank you!
2
0
3.9
10
/** @format */
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const PORT = process.env.PORT || 3000;
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
//connecting to db
try {
mongoose.connect('mongodb://localhost/YOUR_DB_NAME', {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
}, () =>
console.log("connected"));
} catch (error) {
console.log("could not connect");
}
app.get("/", (req, res) => {
res.send("home");
});
app.listen(PORT, () => console.log(`Server is listening on port ${PORT}`));
Thank you!
10
0
4
7
const express = require('express');
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
Thank you!
7
0
0
0
body-parser has been deprecated from express v4.*
Use body-parser package instead.
npm i body-parser
import bodyParser from "body-parser";//for typscript code only, use require for js
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
Thank you!
0
0
4
9
If you are using the latest express module use this:
app.use(express.json())
app.use(express.urlencoded({extended: true}))
Thank you!
9
0
Are there any code examples left?
New code examples in category Javascript