无法在Node.js中使用body-parser与Router。

huangapple go评论78阅读模式
英文:

Unable to use body-parser with Router in nodejs

问题

我正在使用Nodejs和Expressjs进行开发。目前,我尝试在“router”中使用“body-parser”,但我遇到了以下错误 [object object]。如何解决这个问题?以下是我的“api.router.js”文件:

const express = require('express');
const apiController = require('../controllers/api.controller');
const router = express.Router();
var bodyParser = require('body-parser');
// 用于创建新记录的端点
router.post('/new', apiController.addUser);
module.exports = router;

这是我的“api.controller.js”文件:

const User = require('../models/user.model');
const addUser = async (req, res) => {
const { firstName, lastName, age } = req.body;
  console.log(req.body);
  // ...
}
英文:

I am working with Nodejs and using expressjs,Right now i am trying to use "body-parser" with "router" but right now i am getting following error [object object],How can i solve this ? Here is my "api.router.js" file

const express = require('express');
const apiController = require('../controllers/api.controller');
const router = express.Router();
var bodyParser = require('body-parser')
// Endpoint for creating a new record
router.post('/new', apiController.addUser);
module.exports = router;

And here is my "api.controller.js"

const User = require('../models/user.model');
const addUser = async (req, res) => {
const { firstName, lastName, age } = req.body;
  console.log(req.body);
  ...
}

答案1

得分: 1

请从您的代码中删除以下行:

var bodyParser = require('body-parser');

然后,在定义路由之前,将以下代码添加到您的api.router.js文件中:

router.use(express.json());
router.use(express.urlencoded({ extended: true }));
英文:

remove the following line from your code:

var bodyParser = require('body-parser');

And instead, add the following code to your api.router.js file, before defining the routes:

router.use(express.json());
router.use(express.urlencoded({ extended: true }));

答案2

得分: 1

Express/Connect top-level generic
And
Express route-specific

You can use any one of them or conditionally use based on your requirement

英文:

There are two types of usage explained by the author

Express/Connect top-level generic
And
Express route-specific

You can use any one of them or conditionally use based on your requirement

答案3

得分: 0

你可以使用 app.use(bodyParser.json());app.use(express.json());

const express = require("express");
const app = express();
var bodyParser = require('body-parser');
const helloworld_route = require("./routes/helloworld");

const port = process.env.PORT || 2000;

app.use(bodyParser.json());

app.use("/", helloworld_route);

app.listen(port, () => {
  console.log(`监听在端口 ${port}`);
});
英文:

You can use app.use(bodyParser.json()); or app.use(express.json());

const express = require("express");
const app = express();
var bodyParser = require('body-parser')
const helloworld_route = require("./routes/helloworld");

const port = process.env.PORT || 2000;

app.use(bodyParser.json());

app.use("/", helloworld_route);

app.listen(port, () => {
 console.log(`Listening on ${port}`);
});

huangapple
  • 本文由 发表于 2023年4月4日 15:48:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定