我遇到了中间件未给出响应的问题。我已搜索过此问题,但找不到适用的解决方案来解决此问题。
我如何创建中间件
import express from 'express';
const app = express();
app.use(function log(req, res, next) {
console.log('logging with fun...');
next();
});
console.log('Help help help ...');
const port = process.env.PORT || 3000
app.listen(port, () => console.log(`Listening on port: ${port}`));
请指导,我在哪里犯了错误,因为我只在控制台中获得输出,如下所示:
救命救命……
监听端口:3000
最佳答案
您正确添加了中间件,但没有创建任何路由。
Your code will become
import express from 'express';
const app = express();
app.use(function log(req, res, next) {
console.log('logging with fun...');
next();
});
// Just add any route.
app.get('/',function log(req, res) {
res.send('Hello');
});
console.log('Help help help ...');
const port = process.env.PORT || 3000
app.listen(port, () => console.log(`Listening on port: ${port}`));
Now navigate to
http://localhost:3000/
关于node.js - 如何使用 typescript 在 Node 中创建自定义中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50169336/