• 18-Node.js学习笔记-Express-请求处理-构建模块化路由


    构建模块化路由

    ``` const express = require('express') //创建路由对象 const home = express.Router(); //将路由和请求路径进行匹配 app.use('/home',home); //在home路由下继续创建路由 home.get('/index',()=>{ ///home/index res.send('欢迎来到博客展示页面'); }) ```

    基础页面

    ``` //引入express框架 const express = require('express'); //创建网站服务器 const app = express(); //创建路由对象 const home = express.Router(); //为路由对象匹配请求路径 app.use('/home',home); home.get('/index',(req,res)=>{ res.send('欢迎来到博客首页页面') })

    //监听端口
    app.listen(3000);
    console.log('网站服务器启动成功');

    <h3>模块化路由</h3>
    

    //home.js
    const home = express.Router();
    home.get('/index',()=>{
    res.send('欢迎来到博客展示页面');
    });
    module.exports = home;

    //admin.js
    const admin = express.Router();
    admin.get('/index',()=>{
    res.send('欢迎来到博客管理页面');
    });
    module.exports = admin;

    //app.js
    const home = require('./route/home.js');
    const admin = require('./route/admin.js');
    app.use('/home',home);
    app.use('/admin',admin);

    案例
    

    //router/admin.js
    const express = require('express');
    const admin = express.Router();
    admin.get('/index',(req,res)=>{
    res.send('欢迎来到博客管理页面')
    })
    module.exports = admin;

    //router/home.js
    const express = require('express');
    const home = express.Router();
    home.get('/index',(req,res)=>{
    res.send('欢迎来到柠檬不酸博客首页页面')
    })
    module.exports = home;

    //根目录下08.js
    //引入express框架
    const express = require('express');
    //创建网站服务器
    const app = express();

    const home = require('./router/home');
    const admin = require('./router/admin');

    app.use('/home',home);
    app.use('/admin',admin);
    //监听端口
    app.listen(3000);
    console.log('网站服务器启动成功');

  • 相关阅读:
    威联通NAS 网站无法登录,可以ssh情况下重启设备方法
    NSNotification的几点说明
    使用UIDatePicker
    scrollView的几个属性contentSize contentOffset contentInset
    创建标题栏,UINavigationBar的使用
    点击tablecell中的一个按钮,确定cell所在的行
    UITextView被键盘遮挡的处理
    UITableViewCell中的UILabel添加手势没有响应的解决方法
    将NSArray反向排序
    图片的左右摆动
  • 原文地址:https://www.cnblogs.com/foreverLuckyStar/p/12089122.html
Copyright © 2020-2023  润新知