我有一条路线'/ api / users',我的路线定义不会以某种方式处理。我无法发现什么是错的,但每当我尝试从浏览器访问/ api / users时,浏览器都会保持不变。
app.ts
import * as express from "express"
import * as bodyParser from "body-parser"
import * as mongoose from "mongoose"
import {Config as config} from "./config"
let app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}))
//routes:
import * as userRouter from './Routes/userRoutes'
app.use('/api/Users', userRouter.routes);
var server = app.listen(3000, function () {
console.log('server is up');
})
userRoutes.ts
import * as express from "express"
export function routes() : express.Router {
let userRouter = express.Router();
userRouter.route('/')
.get(function(req,res ) {
res.send('blah')
})
return userRouter;
}
答案 0 :(得分:2)
我的TypeScript生锈了,但我相信你将函数routes
传递给app.use
而不是它返回的路由器实例。
app.use('/api/Users', userRouter.routes);
应该成为
app.use('/api/Users', userRouter.routes());