Simple Web ServerSimple Web Server
下载
文档
  • English
  • 简体中文
  • 繁體中文
  • Español
  • Français
  • Português
  • Русский
  • Deutsch
  • 日本
  • 한국어
  • Italiano
  • Українська
  • Azərbaycanca
  • Nederlands
  • Svenska
GitHub
下载
文档
  • English
  • 简体中文
  • 繁體中文
  • Español
  • Français
  • Português
  • Русский
  • Deutsch
  • 日本
  • 한국어
  • Italiano
  • Українська
  • Azərbaycanca
  • Nederlands
  • Svenska
GitHub
    • 服务器配置
    • 应用设置
    • 编辑 config.json
    • 使用 HTTPS
    • 查看日志
    • 从源码构建
    • 来自Chrome的Web Server?
    • 插件介绍
    • 插件清单文件
    • 插件脚本
    • 使用.swshtaccess文件进行高级配置
    • 使用自定义脚本
    • 创建自定义httpRequest程序

创建自定义httpRequest程序 未完成/可能改变

This guide covers how to write a custom httpRequest handler script.

Step 1: create the request

var request = new httpRequest()

Step 2: Setup events

In this step, you can set any headers, the onload event, and the onerror event

Setting a header:

`request.setRequestHeader(headerType, headerValue)`

or

`request.setHeader(headerType, headerValue)`

Setting the load and error functions:

request.onload = function(e) {
    console.log(e.target.body.toString()) //This will log the response body
}

and

request.onerror = function(e) {
    console.error(e)
}

Step 3: Open the request

In this step, the module will process the url and all information needed and then it will create the request. Headers can still be set after this step.

request.open("GET", url, allowInsecureResponse)

allowInsecureResponse: true or false. By default, self signed certificates are blocked. Enable by setting this to true.

Step 4: send the request

In this step you can send data to the client

request.send(data)

If data is not defined, then nothing will be sent. The content length is automatically set data MUST be a string, buffer, or an ArrayBuffer

Stream to a file

You can stream data straight to a file.

When Setting up the events, you can turn this option on by doing the following.

request.setupStreamToFile(res, savePath)

res MUST be defined as res. It is required to do some things with the file system savePath is the path you want to save the file to. This will create missing folders

the onload request body will be a string saying 'The response was written to a file.'

Edit this page on GitHub
Prev
使用自定义脚本