express使用文档


注:用于工作中接口需求并不是要求访问很大的开发

基本结构

该段体现中间件路由保护作用

// 引入 express 框架
const express = require('express')

// 创建网站服务器
const app = express();

app.use('/admin', (req, res, next) => {
   
    // 用户没有登录
    let isLogin = true;
    // 如果用户登录
    if (isLogin) {
   
        // 让请求继续向下执行
        next();
    } else {
   
        // 如果用户没有登录,直接对客户端作出响应
        res.send('您还没有登录,无法访问当前页面');
    }
})


app.get('/admin', (req, res) => {
   
    res.send('您已登录 可以访问当前页面')
})


app.use((req, res, next) => {
   
    // 为客户端响应404状态码以及提示信息
    res.status(404).send('当前访问的页面是不存在的');
})
// 监听端口
app.listen(3000);
console.log('网站服务器启动成功');

next()主要用于中间件走流程

express每次修改需要重启,安排热启动

1

  1. npm install node-dev -D
  2. // package.json 里的script中,配置
  3. “dev”: “node-dev ./bin/www”
  4. // 启动项目
  5. npm run dev

2

npm install nodemon -g

启动

nodemon app.js

读取文件,不存在返回错误信息

// 引入 express 框架
const express = require('express')

// 引入文件模块
const fs = require('fs');

// 创建网站服务器
const app = express();

app.get('/index', (req, res, next) => {
   
    // 创建一个错误实例并抛出
    // throw new Error('程序发生了未知错误');
    fs.readFile('./demo.txt', 'utf8', (err, result) => {
   
        if (err != null) {
   
            // 文件读取失败 向下传递错误对象
            next(err);
        } else {
   
            // 文件读取成功
            res.send(result);
        }
    })
    // res.send('程序正常')
})

// 错误处理中间件
app.use((err, req, res, next) => {
   
    res.status(500).send(err.message);
})

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


构建模块化路由

  1. 在 route 文件夹中构建不同的路由模块,放在不同的文件中
  2. 通过module.exports 将不同路由模块的路由对象导出
  3. 在 app.js 文件中通过 require 将不同路由模块的路由对象导入,同时对导入的路由进行路由匹配
    界面一
const express = require('express');

const admin = express.Router();

admin.get('/index', (req, res) => {
   
    res.send('欢迎来到博客管理页面');
});

// 导出 admin 这个路由对象
module.exports = admin;

界面二

const express = require('express');

const home = express.Router();

home.get('/index', (req, res) => {
   
    res.send('欢迎来到博客首页页面');
});

// 导出 home 这个路由对象
module.exports = home;

app.js

// 引入 express 框架
const express = require('express');

// 创建网站服务器
const app = express();

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

app.use('/home', home);
app.use('/admin', admin);

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

get 参数获取

// 引入 express 框架
const express = require('express');
// 创建网站服务器
const app = express();

app.get('/index', (req, res) => {
   
    // req.query 获取请求参数
    res.send(req.query)
})

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

进入时浏览器地址栏

 http://localhost:3000/index

页面显示
{}
在浏览器栏输入

 http://localhost:3000/index?name=zhangbing&age=20&gender=

页面显示
{‘name’:‘zhangbing’,‘age’:‘20’,‘gender’:‘男’}

post参数请求

express接收post请求参数需要借助第三方包 body-parser
app.js


// 引入 express 框架
const express = require('express');
const bodyParser = require('body-parser');
// 创建网站服务器
const app = express();

app.use(fn ({
   a: 1}));

function fn (obj) {
   
    return function (req, res, next) {
   
    //通过判断来决定服务器输出台打印什么
        if (obj.a == 1) {
   
            console.log(req.url);
        } else {
   
            console.log(req.method);
        }
        next();
    }
}

app.get('/', (req, res) => {
   
    // 接收 post 请求参数
    res.send('ok');
})

app.post('/add', (req, res) => {
   
    res.send(req.body);
})
// 监听端口
app.listen(3000)
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值