我有一个设置,我正在尝试通过app.get('*',函数(req,res,next)来控制注销我的请求和响应{。我是新来的表达和节点,我发现我的日志未定义。
我已经在stackoverflow上阅读了至少10篇帖子,我的中间件解析器需要在路由之前和应用初始化之后。这就是我在这里做的。 任何人都知道这里发生了什么。
console.log("url = ", req.url, " req-body = ", req.body, "res-type = ", res.get('Content-Type'));
上面的行将返回req-body = {}和res-type = undefined
const path = require('path')
const express = require('express')
const bodyParser = require('body-parser');
const host = 'localhost';
const port = process.env.PORT || 3002;
const app = express()
app.use(bodyParser.json());
app.use(bodyParser.text());
app.use(bodyParser.urlencoded({ extended: true }));
const indexPath = path.join(__dirname, '../index.html')
const publicPath = express.static(path.join(__dirname, 'public'))
// DEV SERVER WEBPACK API
if (process.env.NODE_ENV !== 'production') {
const webpack = require('webpack')
const webpackDevMiddleware = require('webpack-dev-middleware')
const webpackHotMiddleware = require('webpack-hot-middleware')
const config = require('../webpack/webpack.dev.config.js')
const compiler = webpack(config)
// WEBPACK MIDDLEEWARE
app.use(webpackHotMiddleware(compiler))
app.use(webpackDevMiddleware(compiler, {
noInfo: true,
publicPath: config.output.publicPath,
stats: { colors: true },
historyApiFallback: true
}))
}
// MIDDLEWARE FOR PRODUCTION TO SERVE FILES DIRECTLY FROM PUBLIC.
app.use('/public', publicPath)
// REQUEST CALLS.
app.get('*', function (req, res, next) {
console.log("url = ", req.url, " req-body = ", req.body, "res-type = ", res.get('Content-Type'));
res.sendFile(indexPath);
})
app.listen(port, host, () =>{
console.log("server ready on port :", port, " and on host : ", host );
});
答案 0 :(得分:0)
也许这可以解决您的问题:
app.post('*', function (req, res, next) {
console.log("url = ", req.url, " req-body = ", req.body, "res-type = ", res.get('Content-Type'));
res.sendFile(indexPath);
})
将get方法更改为post或put方法。