我刚开始使用node.js来构建RESTFul api。我现在正试图用json在帖子中插入数据。
但是当我试图获得req.body总是得到了解决。 所以我在这里查看。看到有人说快递配置应该在路线之前。
我修改了代码后。我甚至无法获得主页。 任何人都可以帮我解决这个问题吗?
之前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;
发布结果
{
"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
/* 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
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
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
const router = express.Router();
router.route('/').post(articleCtrl.articlePost);
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); });
};
答案 0 :(得分:0)
您需要将bodyParser.json
和cors
作为函数调用; app.use(bodyParser.json());
,app.cors()