路由模块化: http://localhost:3001/平台/控制器/操作方法 http://localhost:3001/admin/goods/add
app.js(入口文件):
var express=require('express');//引入模块var admin =require('./routes/admin.js');var home =require('./routes/home.js');var app=new express(); //实例化app.use('/home',home); //前台(路由) http://localhost:3001/homeapp.use('/admin',admin); //后台(路由) http://localhost:3001/adminapp.use('/',home);//默认加载前台(路由)app.listen(3001,'127.0.0.1');
routes/admin.js(后台路由):
var express=require('express');var router = express.Router(); //可使用 express.Router 类创建模块化、可挂载的路由句柄var goods=require('./admin/goods.js');var user=require('./admin/user.js');//配置路由router.use('/goods',goods); // http://localhost:3001/admin/goodsrouter.use('/user',user); // http://localhost:3001/admin/usermodule.exports = router; //暴露这个 router模块
routes/admin/goods.js(后台商品控制器):
var express=require('express');var router = express.Router(); //可使用 express.Router 类创建模块化、可挂载的路由句柄// http://localhost:3001/admin/goodsrouter.get('/',function(req,res){res.send('显示商品首页');});// http://localhost:3001/admin/goods/addrouter.get('/add',function(req,res){res.send('显示商品 增加');});router.get('/edit',function(req,res){res.send('显示商品 修改');});router.get('/delete',function(req,res){res.send('显示商品 删除');});module.exports = router; //暴露这个 router模块