1The package bodyParser is deprecated. You will get this warning with these lines of code:
2
3app.use(bodyparser.json());
4app.use(bodyParser.urlencoded({extended: true}));
5
6If you are using Express 4.16+ you can now replace those lines with:
7
8app.use(express.json());
9app.use(express.urlencoded()); //Parse URL-encoded bodies
1const express = require('express');
2
3app.use(express.urlencoded({ extended: true }));
4app.use(express.json());
5
1If you are using the latest express module use this:
2
3app.use(express.json())
4app.use(express.urlencoded({extended: true}))
1body-parser has been deprecated from express v4.*
2Use body-parser package instead.
3npm i body-parser
4
5import bodyParser from "body-parser";//for typscript code only, use require for js
6app.use(bodyParser.json());
7app.use(bodyParser.urlencoded({ extended: false }));
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