Use Node.js in your end devices(QQ: 796448809)
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 | △ ¹ | △ ¹ |
IoT.js provides HTTP to support HTTP server and client enabling users to receive/send HTTP request easily.
requestListener
{Function}
The requestListener
is a function which is automatically added to the 'request'
event.
Example
var server = http.createServer(function(request, response) {
...
});
options
{Object}
host
{string} A domain name or IP address of the server to issue the request to. Defaults to ‘localhost’.hostname
{string} Alias for host.port
{number} Port of remote server. Defaults to 80.method
{string} A string specifying the HTTP request method. Defaults to ‘GET’.path
{string} Request path. Defaults to ‘/’. Should include query string if any. E.G. ‘/index.html?page=12’. An exception is thrown when the request path contains illegal characters. Currently, only spaces are rejected but that may change in the future.headers
{Object} An object containing request headers.callback
{Function}
response
{http.IncomingMessage}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.
options
{Object}callback
{Function}
response
{http.IncomingMessage}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) {
...
});
A list of HTTP methods supported by the parser as string
properties of an Object
.
This class inherits from net.Server
.
exception
{Error}socket
{net.Socket}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);
This event is emitted when server is closed.
socket
{net.Socket}This event is emitted when new TCP connection is established.
Emitted each time a client requests an HTTP CONNECT
method.
request
{http.IncomingMessage}response
{http.ServerResponse}After request header is parsed, this event will be fired.
The number of milliseconds of inactivity before a socket is presumed to have timed out. Default value is 120000 (2 minutes).
port
{number}host
{string}backlog
{number}callback
{Function}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() {});
callback
{Function}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.
ms
{number}cb
{Function}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();
});
When underlying connection is closed, ‘close’ event is emitted.
This event is fired when no more data to be sent.
This event is emitted when the response has been sent. It does not guarantee that client has received data yet.
data {Buffer |
string} |
callback
{Function}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.
name
{string}Returns name
field of response’s header
name
{string}Removes name
field from response’s header
name
{string}value
{string}Sets response’s header field(name
) to value
. If the field exists, it overwrites the existing value.
ms
{number}cb
{Function}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.
data {Buffer |
string} |
callback
{Function}Sends data
as a response body. callback
will be called when data is flushed.
statusCode
{number}statusMessage
{string}headers
{Object}Sets response’s header. headers
is a map between field and value in header.
This object is created internally and returned from http.request(). It represents an in-progress request whose header has already been queued.
This event is fired when the underlying socket is closed.
callback
{Function}err
{Error}Emitted if something went wrong with making or parsing the request.
This event is emitted after all the data was sent, meaning header and body all finished sending.
response
{http.IncomingMessage}This event is emitted when server’s response header is parsed. ` http.IncomingMessage` object is passed as argument to handler.
socket
{net.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.
data {Buffer |
string} |
callback
{Function}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.
ms
{number}cb
{Function}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.
data {Buffer |
string} |
encoding
{string}callback
{Function}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.
http.IncomingMessage inherits Stream.readable
.
When underlying connection is closed, ‘close’ event is emitted.
This event is fired when no more data to be received.
HTTP header object.
Requests method as string
Underlying socket
HTTP response status code as number
of 3-digit.
HTTP response status message as string
Requests URL as string
ms
{number}cb
{Function}Registers cb for ‘timeout’ event set socket’s timeout value to ms. This event will be triggered by the underlying socket’s ‘timeout’ event.