我是node和express的新手,正试图测试一些基本的路由,但是遇到类型错误:Router.use()需要一个中间件功能,但是有一个对象
这是我在app.js中拥有的
const express = require('express');
const routes = require('./routes/api');
// set up express app
const app = express();
app.use('/api', routes);
//listen for request
app.listen(process.env.port || 4000, function(){
console.log('now listening for requests');
});
以及在我的api.js中:
const express = require('express');
//store router object to a vairable to enable us use routes on api.js
const router = express.Router();
router.get('/ninja', function(req, res){
res.send('{type: "GET"}');
})
router.post('/ninja', function(req, res){
res.send({type: "POST"});
})
//to update an api, where id is a parameter.
router.put('/ninja/:id', function(req, res){
res.send({type: "PUT"});
})
//to delete an api
router.get('/ninja/:id', function(req, res){
res.send({type: "DELETE"});
})
module.exports = router;
我曾尝试在每个文件上导出router
,但都没有效果。