极简Express指迷

作者: 一个顽固的橙子 | 来源:发表于2017-08-16 15:30 被阅读12次

Express

基于 Node.js 平台,快速、开放、极简的 web 开发框架。API
丰富的 HTTP 快捷方法和任意排列组合的 Connect 中间件,让你创建健壮、友好的 API 变得既快速又简单。

express 是一个自身功能极简,完全是由路由和中间件构成一个的 web 开发框架
主要内容如下图所示:

图片来自简书

中间件

中间件(Middleware) 是一个函数,它可以访问请求对象(request object (req
)), 响应对象(response object (res
)), 和 web 应用中处于请求-响应循环流程中的中间件,一般被命名为 next
的变量。如果当前中间件没有终结请求-响应循环,则必须调用 next() 方法将控制权交给下一个中间件,否则请求就会挂起。

应用级中间件

绑定到app对象上,使用app.use()和app.METHOD()
语法:app.use(url, callback)

// 若url为空,即挂载路径为空,应用的每个请求都会执行该中间件
app.use(function (req, res, next) {
  console.log('Time:', Date.now());
  next();
});
// 挂载至 /user/:id 的中间件,任何指向 /user/:id 的请求都会执行它
app.use('/user/:id', function (req, res, next) {
  console.log('Request URL:', req.originalUrl);
  next();
}, function (req, res, next) {
  console.log('Request Type:', req.method);
  next();
});

// 路由和句柄函数(中间件系统),处理指向 /user/:id 的 GET 请求
app.get('/user/:id', function (req, res, next) {
  res.send('USER');
});

路由级中间件

语法:var router = express.Router(), 路由级使用 router.use() 或 router.VERB() 加载。
详细见express.Router的用法

内置中间件-利用 Express 托管静态文件

使用内置中间件 express.static就可以方便的访问静态资源了

app.use(express.static('public')); // public 目录下面的文件就可以访问 `
// 如果你的静态资源存放在多个目录下面,你可以多次调用 express.static 中间件
app.use(express.static('public')); 
app.use(express.static('files'));
app.use('/static', express.static('public')); //通过虚拟路径(static访问)
http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js

错误处理中间件

错误处理中间件有 4 个参数,其签名如下: (err, req, res, next)。

app.use(function(err, req, res, next) {
  console.error(err.stack);
  res.status(500).send('Something broke!');
});

路由

定义应用的端点(URIs)以及如何响应客户端的请求
语法结构:app.METHOD(path, [callback...], callback)
path 是服务器上的路径
METHOD是HTTP请求方法

Express 定义了如下和 HTTP 请求对应的路由方法: get, post, put, head, delete, options, trace, copy, lock, mkcol, move, purge, propfind, proppatch, unlock, report, mkactivity, checkout, merge, m-search, notify, subscribe, unsubscribe, patch, search, 和 connect。

路由回调

可以为请求处理提供多个回调函数,可以利用该机制分配控制权给其他路径
有多种形式,一个函数、多个函数、一个函数数组,或者是两者混合

// 使用一个回调函数
app.get('/example/a', function (req, res) {
  res.send('Hello from A!');
});
// 使用多个回调函数处理路由(记得指定 next 对象)
app.get('/example/b', function (req, res, next) {
  console.log('response ...');
  next();
}, function (req, res) {
  res.send('This is from B!');
});
// 使用回调函数数组处理路由
var c1= function (req, res, next) {
  console.log('C1');
  next();
}

var c2 = function (req, res, next) {
  console.log('C2');
  next();
}
app.get('/example/c', [c1, c2]);
// 混合使用函数和函数数组处理路由:
var cb0 = function (req, res, next) {
  console.log('CB0');
  next();
}

var cb1 = function (req, res, next) {
  console.log('CB1');
  next();
}

app.get('/example/d', [cb0, cb1], function (req, res, next) {
  console.log('response will be sent by the next function ...');
  next();
}, function (req, res) {
  res.send('Hello from D!');
});

app.route()

创建路由路径的链式路由句柄

app.route('/book')
  .get(function(req, res) {
    res.send('Get a random book');
  })
  .post(function(req, res) {
    res.send('Add a book');
  })
  .put(function(req, res) {
    res.send('Update the book');
  });

express.Router

可使用 express.Router 类创建模块化、可挂载的路由句柄。Router 实例是一个完整的中间件和路由系统
在 app 目录下创建名为 router.js 的文件,内容如下:

var express = require('express');
var router = express.Router();

// 该路由使用的中间件
router.use(function timeLog(req, res, next) {
  console.log('Time: ', Date.now());
  next();
});
// 定义网站主页的路由
router.get('/', function(req, res) {
  res.send('home page');
});
// 定义 about 页面的路由
router.get('/about', function(req, res) {
  res.send('About page');
});

module.exports = router;

然后在应用中加载路由模块:

var router = require('./router');
...
router .use('/main', router );

应用即可处理发自 /main和 /main/about 的请求,并且调用为该路由指定的 timeLog 中间件。

相关文章

  • 极简Express指迷

    Express 基于 Node.js 平台,快速、开放、极简的 web 开发框架。API丰富的 HTTP 快捷方法...

  • express相关

    第1章:Express简介 1.1 Express是什么 Express 是一个基于 Node.js 平台的极简、...

  • 课时72-课时77笔记.md

    课时72 Express简介 基于 Node.js 平台,快速、开放、极简的 Web 开发框架 Express中文...

  • 使用express搭建一个在线便利贴

    Express 基于Node.js 平台,快速、开放、极简的 web 开发框架。 Express使用文档 新建项目...

  • nodejs 教程一(helloworld)

    Express 基于Node.js平台,快速、开放、极简的web开发框架 首先安装 Express 创建web项目...

  • Express

    什么是Express Express是基于Node.js 平台,快速、开放、极简的 Web开发框架。 Expre...

  • Node.js + Express + MongoDB实现简易的

    Express是一个极简的Node.js Web框架。Express官网 学习了express之后,简单实现了一个...

  • 基于Node.js的Express框架

    一、什么是Express Express基于Node.js封装的,快速、开放、极简的 Node.js Web 开发...

  • 运行vue打包后的项目

    使用基于 Node.js 平台,快速、开放、极简的 Web 开发框架Express 1、按照express创建出基...

  • express 基础的学习

    1.什么是express? Express是基于 Node.js 平台,快速、开放、极简的 Web 开发框架 2....

网友评论

    本文标题:极简Express指迷

    本文链接:https://www.haomeiwen.com/subject/awyirxtx.html