前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【NPM库】- 0x03 - Express

【NPM库】- 0x03 - Express

作者头像
WEBJ2EE
发布2020-08-13 15:52:20
8960
发布2020-08-13 15:52:20
举报
文章被收录于专栏:WebJ2EEWebJ2EE

1. Express 基础

1.1. 是什么?

Express 是一个保持最小规模的灵活的 Node.js Web 应用程序开发框架,为 Web 和移动应用程序提供一组强大的功能。

1.2. 安装

代码语言:javascript
复制
npm install express --save

1.3. 基础

代码语言:javascript
复制
const path = require("path");
const http = require('http');
const express = require("express");

/**
 * 监听主机
 * 监听端口
 */
const hostname = "localhost";
const port = 3000;

/**
 * 创建 app
 */
const app = express();
const listeningApp = http.createServer(app);

/**
 * 静态文件托管
 */
// 1.   the path that you provide to the express.static function
//      is relative to the directory from where you launch your node process.
//      If you run the express app from another directory,
//      it’s safer to use the absolute path of the directory that you want to serve.
// 2.   Express 会自动在配置的静态目录查找文件。存放静态文件的目录名不会出现在 URL 中。
//
//  例如:http://localhost:3000/img1.jpg
//  例如:http://localhost:3000/some/deep/path/img2.jpg
//
app.use(express.static(path.join(__dirname, 'public')));
// 3.   To create a virtual path prefix
//      (where the path does not actually exist in the file system)
//      for files that are served by the express.static function,
//      specify a mount path for the static directory.
//
//  例如:http://localhost:3000/static/img1.jpg
//  例如:http://localhost:3000/static/some/deep/path/img2.jpg
//
app.use('/static', express.static('src/public'))

/**
 * 路由配置
 */
app.get('/', (req, res) => res.send('Hello World!'))

/**
 * 开始监听
 */
listeningApp.listen(port, hostname, () => console.log(`Example app listening on port ${port}!`));

1.4. webpack-dev-server 对静态资源的处理

webpack-dev-server 的底层是 express,webpack-dev-server 提供的静态资源管理参数有这几个:

  • devServer.contentBase
  • devServer.contentBasePublicPath
  • devServer.staticOptions

2. Express 中间件

2.1. 原理

Middleware functions are functions that have access to the request object (req), the response object (res), and the next function in the application’s request-response cycle. The next function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware.

Middleware functions can perform the following tasks:

  • Execute any code.
  • Make changes to the request and the response objects.
  • End the request-response cycle.
  • Call the next middleware in the stack.

If the current middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging.

示例:

2.2. connect-history-api-fallback

webpack-dev-server 的 historyApiFallback 特性,是借助 express 中间件 connect-history-api-fallback,通过重写 url 实现。

  • 对于满足如下条件的 url,将被重定向到 /index.html
    • The request is a GET request
    • which accepts text/html,
    • is not a direct file request, i.e. the requested path does not contain a . (DOT) character and
    • does not match a pattern provided in options.rewrites (see options below)

connect-history-api-fallback 代码示例:

webpack-dev-server 相关源码:

2.3. http-proxy-middleware

webpack-dev-server 使用 http-proxy-middleware 实现其 proxy 功能。

2.4. serve-index

serve-index 可以将文件夹中文件列表显示到浏览器中。

代码语言:javascript
复制
npm install serve-index

webpack-dev-server 中同样使用 serve-index 生成目录列表,与之相关的配置有:

  • serveIndex
  • contentBase
  • contentBasePublicPath

参考:

express: https://expressjs.com/ https://www.expressjs.com.cn/ express:static-files https://www.expressjs.com.cn/starter/static-files.html express:middleware https://www.expressjs.com.cn/guide/writing-middleware.html connect-history-api-fallback: https://github.com/bripkens/connect-history-api-fallback http-proxy-middleware: https://github.com/chimurai/http-proxy-middleware serve-index: https://github.com/expressjs/serve-index webpack-dev-server:contentBase(3.11.0) https://webpack.js.org/configuration/dev-server/#devservercontentbase https://webpack.js.org/configuration/dev-server/#devservercontentbasepublicpath https://webpack.js.org/configuration/dev-server/#devserverstaticoptions webpack-dev-server:historyApiFallback(3.11.0) https://webpack.js.org/configuration/dev-server/#devserverhistoryapifallback webpack-dev-server:proxy https://webpack.js.org/configuration/dev-server/#devserverproxy

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-08-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 WebJ2EE 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
消息队列 TDMQ
消息队列 TDMQ (Tencent Distributed Message Queue)是腾讯基于 Apache Pulsar 自研的一个云原生消息中间件系列,其中包含兼容Pulsar、RabbitMQ、RocketMQ 等协议的消息队列子产品,得益于其底层计算与存储分离的架构,TDMQ 具备良好的弹性伸缩以及故障恢复能力。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档