ngate
v0.1.0-alpha.1
Published
一个静态文件服务器
Maintainers
Readme
ngate
一个轻量级的静态文件服务器,可以运行静态网站,或者直接将文件夹作为服务提供访问。
功能特点
- 🚀 快速启动静态文件服务
- 📁 支持文件夹目录浏览
- 🔄 支持API代理转发
- 🗜️ 支持Gzip压缩
- 🌐 支持CORS跨域访问
- 💾 支持自定义缓存控制
- 🖥️ 支持自动打开浏览器
- ⚙️ 支持配置文件
安装
全局安装
npm install -g ngate本地安装
npm install ngate使用方法
命令行参数
用法: ngt [选项]
选项:
-V, --version 输出版本号
-p, --port <port> 服务器端口号 (默认: "8080")
-d, --directory <path> 要服务的目录路径 (默认: 当前工作目录)
-c, --compress 启用gzip压缩
--cors 启用CORS支持
--cache <seconds> 设置缓存过期时间(秒)(默认: "3600")
--proxy <urls> 设置代理转发规则,格式如 "/api:http://localhost:3000"
--open 启动服务器后自动打开浏览器
-q, --quiet 静默模式,减少日志输出
--config <path> 指定配置文件路径
--no-config 忽略配置文件,仅使用命令行参数
-h, --help 显示帮助信息基本用法
启动当前目录的文件服务
ngt指定端口和目录
ngt -p 3000 -d ./dist启用压缩和CORS
ngt -c --cors配置代理
ngt --proxy "/api:http://localhost:8000"多个代理规则
ngt --proxy "/api:http://localhost:8000,/auth:http://localhost:9000"使用指定的配置文件
ngt --config ./ngate.config.js忽略配置文件,仅使用命令行参数
ngt --no-config -p 3000配置文件
ngate支持使用配置文件来设置服务器选项。配置文件优先级:
- 命令行参数(最高优先级)
- 通过
--config选项指定的配置文件 - 当前目录中自动发现的配置文件
- 默认配置
支持的配置文件名(自动发现):
ngate.config.jsngate.config.json.ngaterc.ngaterc.json.ngaterc.js
配置文件示例(JavaScript)
// ngate.config.js
module.exports = {
// 服务器端口 (默认: 8080)
port: 3000,
// 静态文件目录 (默认: process.cwd())
directory: './dist',
// 是否启用gzip压缩 (默认: false)
compress: true,
// 是否启用CORS (默认: false)
cors: true,
// 缓存过期时间(秒)(默认: 3600,即1小时)
cacheMaxAge: 86400, // 24小时
// 代理规则 (默认: {})
proxyRules: {
'/api': 'http://localhost:8000',
'/auth': 'http://localhost:9000'
},
// 是否自动打开浏览器 (默认: false)
openBrowser: true
};配置文件示例(JSON)
{
"port": 3000,
"directory": "./dist",
"compress": true,
"cors": true,
"cacheMaxAge": 86400,
"proxyRules": {
"/api": "http://localhost:8000",
"/auth": "http://localhost:9000"
},
"openBrowser": true
}在NPM脚本中使用
可以在package.json的scripts中配置:
{
"scripts": {
"serve": "ngt -d ./dist -p 3000 --open"
}
}在代码中使用
ngate也可以作为模块在代码中使用:
const { ServerManager } = require('ngate');
const server = new ServerManager({
port: 3000,
directory: './public',
compress: true,
cors: true,
cacheMaxAge: 3600,
proxyRules: {
'/api': 'http://localhost:8000'
},
openBrowser: true
});
server.start();使用TypeScript:
import { ServerManager } from 'ngate';
const server = new ServerManager({
port: 3000,
directory: './public',
compress: true,
cors: true,
cacheMaxAge: 3600,
proxyRules: {
'/api': 'http://localhost:8000'
},
openBrowser: true
});
server.start();
// 优雅地关闭服务器
process.on('SIGINT', () => {
server.stop();
process.exit(0);
});兼容性
ngate 要求 Node.js 12.0.0 或更高版本。
贡献
欢迎通过 issue 和 pull request 贡献代码。
许可证
MIT
