[英]body-perser is not parsing my request in express.js
I am trying to use Controller here.我想在这里使用 Controller 。
This is my app.js:这是我的 app.js:
var express = require('express');
var app = express();
const routes = require('./Routes/route');
const bodyParser = require('body-parser');
app.use('/', routes);
// support parsing of application/json type post data
app.use(bodyParser.json());
//support parsing of application/x-www-form-urlencoded post data
app.use(bodyParser.urlencoded({extended: true}));
var server = app.listen(8081);
This is my route.js:这是我的 route.js:
const express = require('express');
const router = express.Router();
const controller = require('../Controllers/controller');
router.get('/counter', controller.showCount);
router.post('/addcount', controller.addCount);
module.exports = router;
This is my controller.js:这是我的 controller.js:
const db = require('../db-connect');
const showCount = (req, res) => {
if(db.state === 'disconnected'){
res.send('Server not connected');
}
else{
db.query('select * from Count', (err, rows, fields) => {
if (err) throw err
res.send(rows);
});
}
};
const addCount = (req, res) => {
console.log(typeof(req.body));
res.send(req.body);
}
module.exports = {showCount, addCount};
Problem: It seems like requests are not being parsed.问题:似乎没有解析请求。 Not sure why.
不知道为什么。
I am using post man to send request.我正在使用邮递员发送请求。 I am sending data as 'form-data' by postman.
我正在通过 postman 将数据作为“表单数据”发送。
you must add app.use('/', routes);
你必须添加
app.use('/', routes);
under the bodyParser middleware bodyParser 中间件下
// support parsing of application/json type post data
app.use(bodyParser.json());
//support parsing of application/x-www-form-urlencoded post data
app.use(bodyParser.urlencoded({extended: true}));
app.use('/', routes);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.