【NPM库】- 0x03 - Express
1. Express 基础
1.1. 是什么?
Express 是一个保持最小规模的灵活的 Node.js Web 应用程序开发框架,为 Web 和移动应用程序提供一组强大的功能。
1.2. 安装
npm install express --save
1.3. 基础
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 可以将文件夹中文件列表显示到浏览器中。
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
- MYSQL | 企业整合解决方案之mysql集群搭建-主从配置
- Spring Cloud中Feign配置详解
- Spring Cloud中Feign的继承特性
- JavaScript 常用方法总结
- Spring Cloud中声明式服务调用Feign
- Spring Cloud中Hystrix仪表盘与Turbine集群监控
- 轻量级压力测试工具 - AB
- Spring Cloud中Hystrix的请求合并
- Spring Cloud中Hystrix的请求缓存
- mysql主从复制配置
- Spring Cloud系列勘误
- Spring Cloud Stream使用细节
- Redis3 对集群进行重新分片
- Spring Cloud Stream初窥
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- GraphQL两年实战避坑经验
- 基于Dubbo的服务提供者与消费者的发布(在虚拟机中)以及使用nginx对项目进行负载均衡优化
- 我们为什么不使用CSS框架
- java.lang.IllegalArgumentException: node to traverse cannot be null!
- Spring Data Jpa 异常:PropertyReferenceException: No property xxx found for type for type yyy
- Spring全家桶之SpringData——SpringData Redis(附相关jar包)
- Spring全家桶之SpringBoot——初级阶段
- Spring全家桶之SpringBoot——高级阶段
- feign.FeignException$MethodNotAllowed: status 405 reading xxx#yyy(Integer)
- No serializer found for class 类名 and no properties discovered to create BeanSerializer
- 三步让你在Linux中发布SpringCloud项目
- 【赵渝强老师】MySQL高可用架构:MHA
- Python终极调试指南
- Spring全家桶之SpringCloud——高级阶段(上)
- go实现利用最大堆寻找最小k个数