browser-server

A HTTP "server" in the browser that uses a service worker to allow you to easily send back your own stream of data.

  • 所有者: mafintosh/browser-server
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

browser-server

A HTTP "server" in the browser that uses a service worker to allow you to easily send back your own stream of data.

npm install browser-server

Usage

First generate the service worker, using the browser-server command line tool

npm install -g browser-server
# /demo is the prefix you want to intercept
browser-server /demo > worker.js

Then create a simple app and browserify it

var createServer = require('browser-server')
var server = createServer()

server.on('request', function (req, res) {
  console.log('intercepting request', req)
  res.end('hello world')
})

server.on('ready', function () {
  fetch('/demo/test.txt').then(function (r) {
    return r.text()
  }).then(function (txt) {
    console.log('fetch returned', txt)
  })
})

Then browserify this app

browserify app.js > bundle.js

And make a index.html page like this

<!DOCTYPE html>
<html>
<body>
<script src="bundle.js"></script>
</body>
</html>

Make sure the worker.js file is also stored in the same folder.

Now serve the folder using a http server, fx

npm install -g http-server
http-server .

Now if you open index.html you should see the server intercepting the request and returning hello world.

Works for all http apis, including video/audio tags!

License

MIT

主要指标

概览
名称与所有者mafintosh/browser-server
主编程语言JavaScript
编程语言JavaScript (语言数: 2)
平台
许可证MIT License
所有者活动
创建于2017-05-18 13:27:49
推送于2018-10-07 03:15:10
最后一次提交2017-05-22 13:01:50
发布数4
最新版本名称v1.0.3 (发布于 2017-05-22 13:01:50)
第一版名称v1.0.0 (发布于 2017-05-18 15:28:59)
用户参与
星数187
关注者数4
派生数11
提交数10
已启用问题?
问题数5
打开的问题数3
拉请求数0
打开的拉请求数2
关闭的拉请求数1
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?