如何使用打字稿在节点中创建自定义中间件
how to create custom middleware in node using typescript
我遇到了中间件没有给出响应的问题。我已搜索过此问题,但找不到解决此问题的适用解决方案。
我是如何创建中间件的
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/
我遇到了中间件没有给出响应的问题。我已搜索过此问题,但找不到解决此问题的适用解决方案。
我是如何创建中间件的
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/