转:express实现多级路由文件

./index.js:

var app = require('express')(); // anything beginning with "/api" will go into this app.use('/api', require('./routes/api')); ///一定要使用use,使用get的的话,无法实现多级路由 app.listen(3000);

./Routs/API/index.js:

var router = require('express').Router(); // split up route handling router.use('/products', require('./products')); router.use('/categories', require('./categories')); // etc. module.exports = router;

./Routs/API/Products.js:

var router = require('express').Router(); // api/products router.get('/', function(req, res) { res.json({ products: [] }); }); // api/products/:id router.get('/:id', function(req, res) { res.json({ id: req.params.id }); }); module.exports = router;

转载于:https://www.cnblogs.com/durcaidurcai/p/10343050.html

你可能感兴趣的:(json)