Object built-in object

ObjectHttpServer

HttpServer is one of the built-in objects that is used to create HTTP servers. An HttpServer object contains two required parameters: port and event processing interface object. In the event processing interface object, the specific implementation method can be a simple callback function, or complex routing, chain processing arrays, etc.

httpThe server object will beTcpServerandHttpHandlerCombining encapsulated objects makes it easy to quickly build a server, which is logically equivalent to:

1 2 3
var svr = new net.TcpServer(addr, port, new http.Handler(function(req) { ... }));

The following is the simplest HttpServer application example, which simply returns the string hello world to all requests.

1 2 3 4 5
const http = require('http'); var svr = new http.Server(8080, (req) => { req.response.write('hello, world'); }); svr.start();

As you can see from the code, first, we imported the built-inhttpmodule. Then we created a new HttpServer object and passed in two necessary parameters: one is the port number, and the other is the specific event processing interface object. In this example, we use a simple callback function as the event processing interface to respond to data from HTTP requests. Which req.response.write('hello, world')is used to respond our string hello world to the client.

After completing the creation of the HttpServer object, use svr.start()to start the server so that we can receive HTTP requests from the Internet through this server.

inheritance relationship

Constructor

HttpServer

HttpServer constructor, listen on all local addresses

1 2
new HttpServer(Integer port, Handler hdlr);

Call parameters:

  • port: Integer, specifiedhttpServer listening port
  • hdlr:Handler,httpBuilt-in message processor, processing function, chain processing array, routing object, see for detailsmq.Handler

HttpServer constructor

1 2 3
new HttpServer(String addr, Integer port, Handler hdlr);

Call parameters:

  • addr: String, specifyhttpServer listening address, if it is "", it will listen on all addresses of the local machine.
  • port: Integer, specifiedhttpServer listening port
  • hdlr:Handler,httpBuilt-in message processor, processing function, chain processing array, routing object, see for detailsmq.Handler

HttpServer constructor

1 2
new HttpServer(String addr, Handler hdlr);

Call parameters:

  • addr: String, specifyhttpServer listening address, if it is "", it will listen on all addresses of the local machine.
  • hdlr:Handler,httpBuilt-in message processor, processing function, chain processing array, routing object, see for detailsmq.Handler

member properties

maxHeadersCount

Integer, query and set the maximum number of request headers, the default is 128

1
Integer HttpServer.maxHeadersCount;

maxHeaderSize

Integer, query and set the maximum request header length, the default is 8192

1
Integer HttpServer.maxHeaderSize;

maxBodySize

Integer, query and set the maximum body size in MB, the default is 64

1
Integer HttpServer.maxBodySize;

enableEncoding

Boolean, automatic decompression function switch, turned off by default

1
Boolean HttpServer.enableEncoding;

serverName

String, query and set the server name, the default is: fibjs/0.x.0

1
String HttpServer.serverName;

socket

Socket, the server is currently listening toSocketobject

1
readonly Socket HttpServer.socket;

handler

Handler, the current event processing interface object of the server

1
Handler HttpServer.handler;

member function

enableCrossOrigin

Allow cross-domain requests

1
HttpServer.enableCrossOrigin(String allowHeaders = "Content-Type");

Call parameters:

  • allowHeaders: String, specified to be acceptedhttpheader field

start

Start current server

1
HttpServer.start();

stop

Close the socket to abort the running server

1
HttpServer.stop() async;

toString

Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.

1
String HttpServer.toString();

Return results:

  • String, returns the string representation of the object

toJSON

Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.

1
Value HttpServer.toJSON(String key = "");

Call parameters:

  • key: String, not used

Return results:

  • Value, returns a value containing JSON serializable