返回信息流学前端过程中经常要用个http server去托管一下静态文件,我一般是一句python3 -m http.server搞定,但是有时候要控制http header就略蛋疼了(比如cors/csp相关的东西). 虽然单纯的说返回一个特定的header并不难,但是要搭个后台还是麻烦了点.
于是造了一个命令行工具httpany,可以根据querystring去设置响应的状态码和header.
安装方法
在有node 4,5,6其一以及npm的情况下
npm install httpany -g
使用方法
$ httpany webdir -p 8000
这样就把webdir目录映射到http://localhost:8000/了
访问 http://localhost:8000/?foo=bar&author=flowmemo ,返回响应的header里就会有foo:bar和author:flowmemo这两项.
访问 http://localhost:8000/?status=301&Location=//weibo.com/flowmemo 就会返回一个302跳转到我的微博主页.
实现原理
用的是koa v1,koa-static(托管静态文件用),和koa-httpany(为这个工具造的koa中间件). koa不用v2是因为v2依赖了babel, 装起来太慢了,而我这个工具实现功能又很简单,核心代码不到100行,没必要那么重(koa-http是支持koa v2的)
使用场景
这个工具很单纯,静态文件服务器加个控制http状态码和header功能. 比如前端学习过程中想了解某个http header对自己写的页面的影响,用这个快速调试还是很方便的. 复杂一点的那肯定会写后台了也不需要我这个工具.
项目地址
httpany: https://github.com/flowmemo/httpany
依赖的中间件koa-httpany: https://github.com/flowmemo/koa-httpany
评论区@binux说的http://httpbin.org 这个网站也可以控制返回的请求,有响应的python包(不太确定是否带托管静态文件的功能)
————
微博 @flowmemo , 现在主要写JavaScript. 关注广泛, 欢迎交流.
此签名通过「北邮人签名档」脚本发送
这是一条镜像帖。来源:北邮人论坛 / www-technology / #37711同步于 2016/7/15
该镜像源已超过 30 天没有更新,可能在源站已被删除。
WWWTechnology机器人发帖
造了个方便前端学习者的静态服务器工具,可自定义header和状态
e97ace
2016/7/15镜像同步12 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
顺便贴一下Readme,测试一下论坛的markdown
# httpany
> [English](https://github.com/flowmemo/httpany/blob/master/README.md)|**中文**
方便前端学习者使用的静态文件服务器
[https://img.shields.io/travis/flowmemo/httpany/master.svg?style=flat-square](https://travis-ci.org/flowmemo/httpany)
[https://img.shields.io/appveyor/ci/flowmemo/httpany/master.svg?style=flat-square&label=Win%20Test](https://ci.appveyor.com/project/flowmemo/httpany/branch/master)
[https://img.shields.io/coveralls/flowmemo/httpany/master.svg?style=flat-square](https://coveralls.io/github/flowmemo/httpany?branch=master)
## 介绍
前端学习者经常需要在电脑上托管静态文件.
有时候一句 `python3 -m http.server` 就足够了, 但是很多情况下你会想让服务器返回特定的http header(例如CORS和Timing-Allow).
这个工具可以让你通过querystring来控制服务器响应的http header和状态码.
## 示例
```shell
$ npm install httpany -g
$ httpany yourDirectory
# "yourDirectory" is served on http://localhost:3000
# options is: {"root":"/path/to/yourDirectory","index":"index.html"}
```
访问 http://localhost:3000/?foo=bar , http响应的header中会包含 `foo: bar`.
访问 http://localhost:3000/?status=302&location=//weibo.com/flowmemo , 会返回一个302页面跳转到我的微博.
你也可以在querystring中设置status来控制响应的http状态码.
## 用法
```shell
$ httpany <路径> [参数]
```
### 参数
--port, -p 监听端口,默认是3000
--help, -h 显示帮助
下面的参数来自koa-static:
--maxage Browser cache max-age in milliseconds. defaults to 0
--hidden Allow transfer of hidden files. defaults to false
--index Default file name, defaults to 'ipndex.html'
--defer If true, serves after return next(), allowing any downstream middleware to respond first.
--gzip Try to serve the gzipped version of a file automatically when gzip is supported by a client and if the requested file with .gz extension exists. defaults to true.
## 注意
`Access-Control-Allow-Origin` 默认设置为`*`. 你可以把此项设置为`null`来禁用它.
## 许可
MIT (C) [flowmemo](http://weibo.com/flowmemo)
好流弊,其实需要改什么服务器返回的话,一般我会用mitmproxy,
下面这个tamper好像很好用的样子,然而我并没跑起来。。。
https://chrome.google.com/webstore/detail/tamper/mabhojhgigkmnkppkncbkblecnnanfmd
【 在 reverland 的大作中提到: 】
: 好流弊,其实需要改什么服务器返回的话,一般我会用mitmproxy,
: 下面这个tamper好像很好用的样子,然而我并没跑起来。。。
: https://chrome.google.com/webstore/detail/tamper/mabhojhgigkmnkppkncbkblecnnanfmd
看起来功能很强大的,mark一下
————
微博 @flowmemo , 现在主要写JavaScript. 关注广泛, 欢迎交流.
此签名通过「北邮人签名档」脚本发送
http://httpbin.org/response-headers?foo=bar&author=flowmemo
http://httpbin.org/redirect-to?url=//weibo.com/flowmemo