路由?"/>
为什么在以下代码中不执行路由?
我想让服务器console log
在第三app.use
中接收到的任何内容都被提取,但是这使路由没有执行。为什么?这是否不是对任何请求进行一般响应的理想方式?。]
const express = require('express');
const bodyParser = require('body-parser');
const bbdb = require('./database/bbDB');
const sql = require('./database/sql');
const app = express();
const port = process.env.PORT || 5000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); //keep this to true to be able to post nested object
app.use((json)=>{
console.log('receiving fetch at: ' + json.originalUrl);
});
app.get('/api/hello', (req, res) => {
res.send({express: 'Hello'});
});
app.get('/api/hello/data', async (req, res) =>{
try{
console.log('receiving fetch at: ' + req.originalUrl);
const reqQuery = req.query;
let results = await bbdb.queryProducts(sql.all_home_desc);
res.send(results);
} catch(err) {
console.log(err);
res.sendStatus(500);
}
});
app.listen(port, () => console.log(`Listening on port ${port}`));
回答如下:您必须手动调用下一个中间件。尝试以下操作:
app.use((req, res, next) => {
console.log(`receiving fetch at: ${req.url}`);
next();
});
更多推荐
为什么在以下代码中不执行路由?
发布评论