【发布时间】:2021-08-17 17:18:16
【问题描述】:
我正在将此中间件添加到我的 express 应用中
app.use(function(req: express.Request, res: express.Response, next: express.NextFunction) {
req.rawBody = 'hello2';
next();
});
并添加custom.d.ts
declare namespace Express {
export interface Request {
rawBody: string;
}
}
VS Code 没有显示错误,我可以运行此命令
tsc && node --unhandled-rejections=strict ./dist/app.js
但我想使用nodemon 来监听文件更改。所以我把这个脚本加到package.json
"dev": "nodemon app.ts",
然后它开始抛出这个错误
“Request
为了消除错误,我将其添加到 tsconfig.json 文件中
"files": ["custom.d.ts"],
"ts-node": {
"files": true
}
现在 nodemon 可以正常运行,但是 VS Code 显示同样的错误。
我努力了好几个小时才能让它们一起工作。任何帮助表示赞赏。
更新:目前我正在使用tsc-watch 包,它给了我想要的结果。
"dev-server": "tsc-watch --noClear -p ./tsconfig.json --onSuccess \"node ./dist/app.js\"",
或者,如果我分别运行这两个脚本,我会得到想要的结果。这种方法的问题在于,只要代码中出现错误,nodemon 就不会检测到它,因为它正在为 js 文件提供服务。虽然 tsc 命令检测代码中的错误,但您很容易错过这一点,直到您在控制台中检查错误。
{...
"watch": "tsc -w",
"start2": "nodemon ./dist/app.js",
...}
【问题讨论】:
标签: node.js typescript express nodemon