admin管理员组文章数量:815076
为什么在以下代码中不执行路由?
我想让服务器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();
});
本文标签: 为什么在以下代码中不执行路由
版权声明:本文标题:为什么在以下代码中不执行路由? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/biancheng/1715197654a822344.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论