为什么在以下代码中不执行路由?

编程入门 行业动态 更新时间:2024-10-09 09:11:56

为什么在以下代码中不执行<a href=https://www.elefans.com/category/jswz/34/1771390.html style=路由?"/>

为什么在以下代码中不执行路由?

我想让服务器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();
});

更多推荐

为什么在以下代码中不执行路由?

本文发布于:2024-05-07 13:13:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756401.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   中不   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!