Skip to content

Latest commit

 

History

History
95 lines (76 loc) · 1.77 KB

课时6 路由规划.md

File metadata and controls

95 lines (76 loc) · 1.77 KB

路由规划

我们已经把设计的构想图贴出来了,接下来的任务就是完成路由规划了。路由规划,或者说控制器规划是整个网站的骨架部分,因为它处于整个架构的枢纽位置,相当于各个接口之间的粘合剂,所以应该优先考虑。

  • / :首页
  • /users/login :用户登录
  • /users/reg :用户注册
  • /articles/post :发表文章
  • /articles/logout :登出

增加路由

routes/users.js 中添加下列代码

/**
 * 用户注册
 */
router.get('/reg', function (req, res) {
    res.render('user/reg', {title: '注册'});
});

/**
 * 当填写用户注册信息提交时的处理
 */
router.post('/reg', function (req, res) {
});

/**
 * 显示用户登录表单
 */
router.get('/login', function (req, res) {
    res.render('user/login', {title: '登录'});
});

/**
 * 当填写用户登录信息提交时的处理
 */
router.post('/login', function (req, res) {
});

router.get('/logout', function (req, res) {
});

routes/articles.js 中添加下列代码

router.get('/add', function (req, res) {
    res.render('article/add', { title: '发表文章' });
});

router.post('/add', function (req, res) {

});

修改app.js

app.js中添加以下代码

var articles = require('./routes/article');
app.use('/articles', articles);

增加模板

views下增加以下文件

var articles = require('./routes/article');
app.use('/articles', articles);

views/article/add.ejs

<%= title %>

views/user/login.ejs

<%= title %>

views/user/reg.ejs

<%= title %>

效果

用户注册

http://localhost:3000/users/reg image

用户登陆

http://localhost:3000/users/login image

发表文章

image