[英]node.js req.body undefined with express 4.16.X, body-Parser 1.X
I just starting to use node.js to build a RESTFul api. 我刚刚开始使用node.js构建RESTFul api。 I'm now trying to insert data by post with json in body.
我现在正在尝试通过正文中带有json的帖子插入数据。
But when I try to get req.body always got undifined. 但是,当我尝试获得要求时,身体总是没有定义。 So I check at here.
所以我在这里检查。 Saw some people said that the express configuration should before the route.
看到有人说快递配置应该在路线之前。
After I modified my code. 修改代码后。 I can't even get home page.
我什至无法获得主页。 Can anyone help me solve this problem?
谁能帮我解决这个问题?
express.js before 之前的express.js
/* express.js */
import bodyParser from 'body-parser';
import express from 'express';
import cors from 'cors';
import morgan from 'morgan';
import config from './config';
import index from '../server/routes/index.route';
const app = express();
/* GET home page. */
app.get('/', (req, res) => {
res.send(`server started on port.. http://127.0.0.1:${config.port} (${config.env})`);
});
app.use('/api', index);
// parse body params and attache them to req.body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors);
// Http request logger
app.use(morgan('dev'));
export default app;
POST RESULT 发布结果
{
"code": "ER_PARSE_ERROR",
"errno": 1064,
"sqlMessage": "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1",
"sqlState": "42000",
"index": 0,
"sql": "INSERT INTO Article SET ?"
}
express.js after 之后的express.js
/* express.js */
/* ... */
const app = express();
// parse body params and attache them to req.body
app.use(bodyParser.json);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors);
// Http request logger
app.use(morgan('dev'));
/* GET home page. */
app.get('/', (req, res) => {
res.send(`server started on port.. http://127.0.0.1:${config.port} (${config.env})`);
});
app.use('/api', index);
export default app;
index.js index.js
import config from './config/config';
import app from './config/express';
if (!module.parent) {
// listen on port config.port
app.listen(config.port, () => {
console.log(`index.js >>> server started on port http://127.0.0.1:${config.port} (${config.env})`);
});
}
export default app;
index.route.js index.route.js
import express from 'express';
import mysql from 'mysql';
import article from './article.route';
import config from './../../config/config';
const router = express.Router();
/* GET localhost:[port]/api page. */
router.get('/', (req, res) => {
res.send(`此路徑是: localhost:${config.port}/api`);
});
/* mysql連線測試 */
router.get('/sqlTest', (req, res) => {
const connectionPool = mysql.createPool({
connectionLimit: 10,
host: config.mysqlHost,
user: config.mysqlUserName,
password: config.mysqlPass,
database: config.mysqlDatabase
});
connectionPool.getConnection((err, connection) => {
if (err) {
res.send(err);
console.log('連線失敗!');
} else {
res.send('連線成功!');
console.log(connection);
}
});
});
// article router
router.use('/article', article);
export default router;
article.route.js article.route.js
const router = express.Router();
router.route('/').post(articleCtrl.articlePost);
article.controller.js article.controller.js
const articlePost = (req, res) => {
const insertValues = req.body;
console.log('insertValues ', insertValues);
articleModule.createArticle(insertValues).then((result) => {
res.send(result);
}).catch((err) => { return res.send(err); });
};
you need to call bodyParser.json
and cors
as a function; 您需要调用
bodyParser.json
和cors
作为函数; app.use(bodyParser.json());
, app.cors()
,
app.cors()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.