Output : Http server output

更新时间:
2024-05-13
下载文档

Output : Http server output

HttpOutput is the base calss of HttpClientRequest and HttpServerResponse.

The client initiates a request to the server through the output object. The server replies to the client through the output object.

Support

The following shows HttpOutput module APIs available for each permissions.

 User ModePrivilege Mode
output.method
output.path
output.statusCode
output.statusMessage
output.setHeader
output.getHeader
output.writeHead
output.removeHeader
output.addHeaders
output.clearHeaders
output.status
output.write
output.end
output.destroy
output.connected

HttpOutput Object

output.method [for REQUEST]

  • method {String} Contains a string corresponding to the HTTP method of the request. It is expressed in uppercase and is case sensitive.

See httpinput input.method for detail: input.method.

output.path [for REQUEST]

  • path {String} HTTP request url query path.

output.statusCode [for RESPONSE]

  • statusCode {Integer} HTTP response status.

See httpinput input.statusCode for detail: input.method.

output.statusMessage [for RESPONSE]

  • statusMessage {String} HTTP response status message.

See httpinput input.statusCode for detail: input.method.

output.setHeader(key, val)

  • key {String} HTTP header key.
  • val {String} HTTP header value.

Set an HTTP header.

Example

res.setHeader('content-type', 'text/html');

output.getHeader(key)

  • key {String} HTTP header key.
  • Returns: {String|undefined} HTTP header value.

Get an HTTP header. If not exist, return undefined.

output.writeHead(headOpt[, headers])

  • headOpt {Object | Integer | String} HTTP header line message.
    • {Integer} [for RESPONSE] is equivalent to object: '{statusCode: {Integer}}'.
    • {String} [for REQUEST] is equivalent to object: '{method: {String}}'.
    • {Object} Option:
      • statusCode {Integer} [for RESPONSE] HTTP status code.
      • reason {String} [for RESPONSE] HTTP status code message, default: status code message.
      • method {String} [for REQUEST] HTTP request method.
      • path {String} [for REQUEST] HTTP request url query path. .default: '/'.
  • headers {Object} HTTP headers. eg. { 'key': 'value' }

Set output HTTP header line and headers.

Example

res.writeHead(304, 'Not Modified');

output.removeHeader(key)

  • key {String} HTTP header key.

Remove a header from HTTP headers.

Example

res.removeHeader('content-type');

output.addHeaders(headers)

  • headers {Object} HTTP headers.

Set HTTP headers. Copy all object from headers to output headers.

Example

res.addHeaders({'content-type': 'text/html'});

output.clearHeaders()

Clear all HTTP headers.

Example

res.clearHeaders();

output.status([status])

  • status {Integer} HTTP status.
  • Returns: {HttpOutput | Integer} return this object for set operate or status code for get operate.

Set or get HTTP status. If status undefined, this method return output statusCode. Otherwise, set output statusCode.

Example

res.status(200);

output.write(chunk)

  • chunk {String | Number | Boolean | Object | Buffer} Http body data.
  • Returns: {Boolean} true : success. false: fail.

Send data to client/server. If Content-Length not set, output.write() set 'Transfer-Encoding' to 'chunked', and this method can call multiple times. After write all data, user should call output.end() to end output.

output.end([chunk])

  • chunk { String | Number | Boolean | Object | Buffer} Http body data. default: undefined.

If chunk is not empty, the chunk is sent to the client/server and the output is ended. After the outout is finished, continuing to send data is invalid.

The output automatically sets the Content-Type header entry based on the chunk type. Content-Type is set as follows:

chunk typeContent-Type
StringREQUEST: text/plain; RESPONSE: text/html
Number, Boolean, Objectapplication/json
Bufferapplication/octet-stream

output.destroy([error])

  • error {Error} which will be passed as payload in 'error' event.
  • Returns: {HttpOutput} This object.

When this method closes the writable stream, it also closes the underlying socket connection.

output.connected()

  • Returns: {Boolean} Whether the current connection is connected.

Get whether the current network connection is connected.

Example

var connected = output.connected();
文档内容是否对您有所帮助?
有帮助
没帮助