📜  不推荐使用 express body-parser - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:15.678000             🧑  作者: Mango

代码示例6
/** @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}`));