1.0.10 • Published 1 year ago

@tanshenghu/http v1.0.10

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Http Or Https


说明:

  • 周末闲得蛋疼就写了一个web http服务。
  • 启一个本地简易版静态服务:http 或 https服务
  1. 支持自定义端口http、https服务的启用(默认端口为:8080)
  2. 原创纯原生代码,无依赖,只有8KB
  3. 默认就支持跨域服务
  4. 支持SPA模式
  5. 支持指定某一个子目录当服务器根Root目录
  6. 默认支持include语法
  7. 支持proxy跨域代理
  8. 支持外部中间件扩展处理

安装

npm install @tanshenghu/http -g

应用

// 终端进入将要开启web服务的目录中,然后执行如下命令:
http

// 指定端口
http port=4444

// 开启https
http https

// 启用SPA模式(React)
http spa

// 指定项目子目录 dist 是当前目录下面的子目录
http dir=dist

// 支持跨域代理
http proxy=https://www.baidu.com

// 支持跨域代理 所有请求带token头 多个头信息用加号 + 分隔连接起来
// 加号与值左右都不能出现空格
// 如果某一些场景值有空格,请配置_middleTask.js文件加头信息
http proxy=https://www.baidu.com headers=content-type:application/json+token:userTokenValue

功能

// index.html
<html>
  <head>
    <title>DEMO</title>
  </head>

  <body>
    <!-- #include("/header.html"); -->
    <main>
      主干(Main)
    </main>
    <!-- #include("/footer.html"); -->
  </body>
</html>

// header.html
<header>
  <nav>
    <a>首页(Home)</a>
    <a>公司介绍(About)</a>
    <a>联系我们(Contact)</a>
  </nav>
</header>

// footer.html
<footer>
  <p>公司系统版权、站点备案号</p>
</footer>

_middleTask.js

// 在web服务根目录中添加一个 _middleTask.js 文件

module.exports = {
  onRequest: ({context, request, response}) {
    // ...
    if (request.url === '/home.html') {
      // Custom Page   自定义一个虚拟的 home.html 文件页面
      response.end('Hello 福虎 <tanshenghu@163.com>');
      return false;
    }
  },
  onRender: async ({context, content, request, response}) => {
    // ...
    return '<h1>Custom HTML Content...</h1><h2>自定义的HTML文档内容,也可用于调接口数据动态渲染HTML做一个SSR服务</h2>';
  }
}

Appreciate

赞赏码

Author

TanShenghu (福虎)

1.0.10

1 year ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago