在dev-server.js中添加下面代码
var apiRoutes = express.Router();
var db=require("../db.json")
var name=db.name
apiRoutes.get('/json',function (req,res) {
res.json({
error:0,
data:name
});
});
//最后不要忘了 app.use一下
app.use('/api',apiRoutes);
over!
顺便提一下
var router=express.Router()
官网上的例子
// invoked for any requests passed to this router
router.use(function(req, res, next) {
// .. some logic here .. like any other middleware
next();
});
// will handle any request that ends in /events
// depends on where the router is "use()'d"
router.get('/events', function(req, res, next) {
// ..
});
...
app.use('/calendar', router);
官当文档
app.use([path,] callback [, callback...])
Mounts the specified middleware function or functions at the specified path: the middleware function is executed when the base of the requested path matches path
挂载特殊的中间件在路径上,当路径匹配时,执行中间件函数
.