ShadowNode

Use Node.js in your end devices(QQ: 796448809)

View the Project on GitHub yodaos-project/ShadowNode

Platform Support

The following shows Http module APIs available for each platform.

  Linux
(Ubuntu)
Raspbian
(Raspberry Pi)
NuttX
(STM32F4-Discovery)
TizenRT
(Artik053)
http.createServer O O △ ¹ △ ¹
http.request O O △ ¹ △ ¹
http.get O O △ ¹ △ ¹
  1. On NuttX/STM32F4-Discovery and TizenRT/Artik053, even a couple of sockets/server/requests might not work properly.

Http

IoT.js provides HTTP to support HTTP server and client enabling users to receive/send HTTP request easily.

http.createServer([requestListener])

The requestListener is a function which is automatically added to the 'request' event.

Example

var server = http.createServer(function(request, response) {
  ...
});

http.request(options[, callback])

Example

var http = require('http');

var request = http.request({
  method: 'POST',
  port: 80,
  headers: {'Content-Length': 3}
});

...

request.end();

Note that in the example req.end() was called. With http.request() one must always call req.end() to signify that you’re done with the request - even if there is no data being written to the request body.

http.get(options[, callback])

Same as http.request except that http.get automatically call req.end() at the end.

Example

var http = require('http');

http.get({
  port: 80,
}, function(res) {
...
});

http.METHODS

A list of HTTP methods supported by the parser as string properties of an Object.

Class: http.Server

This class inherits from net.Server.

Event: ‘clientError’

If a client connection emits an ‘error’ event, it will be forwarded here. Listener of this event is responsible for closing/destroying the underlying socket.

Default behavior is to destroy the socket immediately on malformed request.

Example

var http = require('http');

var server = http.createServer(function(req, res) {
  res.end();
});

server.on('clientError', function(err, socket) {
  socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});

server.listen(8000);

Event: ‘close’

This event is emitted when server is closed.

Event: ‘connection’

This event is emitted when new TCP connection is established.

Event: ‘connect’

Emitted each time a client requests an HTTP CONNECT method.

Event: ‘request’

After request header is parsed, this event will be fired.

server.timeout

The number of milliseconds of inactivity before a socket is presumed to have timed out. Default value is 120000 (2 minutes).

server.listen(port[, hostname][, backlog][, callback])

Wait for new TCP connection with specified port and hostname. If no hostname is provided, server accepts any IP address. backlog is maximum pending connections. Default backlog length is 511 (not 512). callback will be called when server has been bound.

Example

var http = require('http');

var server = http.createServer(function(req, res) {
  res.end();
});
server.listen(8080, function() {});

server.close([callback])

Stop accepting new connection to this server. However, the existing connections are preserved. When server is finally closed after all connections are closed, a callback is called.

server.setTimeout(ms, cb)

Registers cb for 'timeout' event and sets socket’s timeout value to ms. This event will be triggered by the underlying socket’s ‘timeout’ event.

If cb is not provided, the socket will be destroyed automatically after timeout. If you provide cb, you should handle the socket’s timeout.

Default timeout for server is 2 minutes.

Example

var http = require('http');

var server = http.createServer();
server.setTimeout(100, function(socket) {
  socket.destroy();
  server.close();
});

Class: http.ServerResponse

Event: ‘close’

When underlying connection is closed, ‘close’ event is emitted.

Event: ‘end’

This event is fired when no more data to be sent.

Event: ‘finish’

This event is emitted when the response has been sent. It does not guarantee that client has received data yet.

response.end([data][, callback])

Finishes sending the response.

If data is provided, it sends data first, and finishes. If callback is specified, it is called when the response stream is finished.

response.getHeader(name)

Returns name field of response’s header

response.removeHeader(name)

Removes name field from response’s header

response.setHeader(name, value)

Sets response’s header field(name) to value. If the field exists, it overwrites the existing value.

response.setTimeout(ms, cb)

Registers cb for ‘timeout’ event and set socket’s timeout value to ms. This event will be triggered by the underlying socket’s ‘timeout’ event.

response.write(data[, callback])

Sends data as a response body. callback will be called when data is flushed.

response.writeHead(statusCode[, statusMessage][, headers])

Sets response’s header. headers is a map between field and value in header.

Class: http.ClientRequest

This object is created internally and returned from http.request(). It represents an in-progress request whose header has already been queued.

Event: ‘close’

This event is fired when the underlying socket is closed.

Event: ‘error’

Emitted if something went wrong with making or parsing the request.

Event: ‘finish’

This event is emitted after all the data was sent, meaning header and body all finished sending.

Event: ‘response’

This event is emitted when server’s response header is parsed. ` http.IncomingMessage` object is passed as argument to handler.

Event: ‘socket’

This event is emitted when a socket is assigned to this request. net.Socket object is passed as argument to handler.

After response header is parsed, this event will be fired.

request.end([data][, callback])

Finishes sending the request. If the request is chunked, this will send the terminating '0\r\n\r\n'.

If data is provided, it sends data first, and finishes. If callback is specified, it is called when the request stream is finished.

request.setTimeout(ms, cb)

Registers cb for ‘timeout’ event and set socket’s timeout value to ms. This event will be triggered by the underlying socket’s ‘timeout’ event.

If cb is not provided, the socket will be destroyed automatically after timeout. If you provides cb, you should handle the socket’s timeout.

request.write(data[, encoding][, callback])

Sends a chunk of the body. By calling this method many times, a request body can be sent to a server — in that case it is suggested to use the ['Transfer-Encoding', 'chunked'] header line when creating the request.

The encoding argument is optional and only applies when chunk is a string. Defaults to 'utf8'.

The callback argument is optional and will be called when this chunk of data is flushed.

Class: http.IncomingMessage

http.IncomingMessage inherits Stream.readable.

Event: ‘close’

When underlying connection is closed, ‘close’ event is emitted.

Event: ‘end’

This event is fired when no more data to be received.

message.headers

HTTP header object.

message.method

Requests method as string

message.socket

Underlying socket

message.statusCode

HTTP response status code as number of 3-digit.

message.statusMessage

HTTP response status message as string

message.url

Requests URL as string

message.setTimeout(ms, cb)

Registers cb for ‘timeout’ event set socket’s timeout value to ms. This event will be triggered by the underlying socket’s ‘timeout’ event.