多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
一、路由: ![](https://box.kancloud.cn/9e0f69564b13d110c4923b8216562c1e_836x462.png) index.js ~~~ var express = require('express'); var router = express.Router(); /* GET home page. */ router.get('/', function(req, res, next) { res.render('index', { title: 'Express' }); }); router.get('/helloworld', function(req, res, next) { res.render('helloworld', {}); }); module.exports = router; ~~~ user.js ~~~ var express = require('express'); var router = express.Router(); /* GET users listing. */ router.get('/', function(req, res, next) { res.send('respond with a resource'); }); module.exports = router; ~~~ 如果在浏览器中访问localhost:3000,index.js中以下方法会响应 router.get('/', function(req, res, next) { res.render('index', { title: 'Express' }); }); 如果在浏览器中访问localhost:3000/helloworld,index.js中以下方法会响应 router.get('/helloworld', function(req, res, next) { res.render('helloworld', {}); }); 如果在浏览器中访问localhost:3000/users,user.js中以下方法会响应 router.get('/', function(req, res, next) { res.send('respond with a resource'); }); 如果是post请求, router.post(); 二、静态资源 app.js中以下代码负责配置静态资源,代表静态资源在public ~~~ app.use(express.static(path.join(__dirname, 'public'))); ~~~ ![](https://box.kancloud.cn/54f61e9fcc1399cab59ad1fbf0f81e2c_228x401.png) 访问静态资源路径如下: http://localhost:3000/html/helloword.html http://localhost:3000/stylesheets/style.css 在路由中跳转静态页面: ~~~ router.get('/helloworld', function(req, res, next) { res.redirect("html/helloworld.html"); }); ~~~