我是打字和Exress的新手。我得到了一个简单的导出函数,如下所示:
export function testFunction(req: any, res: any) {
console.log(req.body);
return res.status(200).send('OK');
};
一个简单的导出路径如下:
router.post('/', async (req: any, res: any) => {
console.log(req.body);
return res.status(200).send('OK');
});
module.exports = router;
这是我的服务器代码:
import * as express from 'express';
import {testFunction} from './rpcs/testfunction';
var bodyParser = require('body-parser');
var testRoute = require('./rpcs/testroute');
export function initApplication() {
const app = express();
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use((req: express.Request, res: express.Response, next: express.NextFunction) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "*")
res.header("Access-Control-Allow-Headers", "*");
if ('OPTIONS' === req.method) {
res.sendStatus(200);
} else {
next();
}
})
app.get("/api/test", function (req:any, res:any) {
res.send("Hello World!");
});
app.post("/api/testfunction", testFunction);
app.use("/api/testroute", testRoute);
return app;
}
它工作得很好。但当我改变时,我不明白的是
app.use("/api/testroute", testRoute);
到
app.post("/api/testroute", testRoute);
错误将被发送出去。。。
Cannot POST /api/testroute
在旧的node.js项目中,我可以很好地做到这一点。但是当我将这个项目更新为打字稿时。这种错误会发生。有人能解释一下原因吗
使用
只能和
路由器
和
邮递
只能和
功能
??