ObjectHttpsServer
HttpsServer is an object used to create an https server. The HttpsServer object can be usedHttpServerAll interface functions and properties. The HttpsServer object can be created by passing in the certificate object previously generated using openssl (X509Certtype) and key object (PKeytype), thus providing the client withsslCryptographically protected services
The https server object is theSslServerandHttpHandlerCombining encapsulated objects makes it easy to quickly build a server, which is logically equivalent to:
1
2
3var svr = new net.SslServer(crt, key, addr, port, new http.Handler(function(req) {
...
}));
Here is a sample code using HttpsServer:
1
2
3
4
5
6
7
8
9
10
11
12const http = require("http");
const crypto = require("crypto");
// load cert and key
const cert = crypto.loadCert("server.crt");
const key = crypto.loadPKey("server.key");
// create https server
const server = new http.HttpsServer(cert, key, 8443, function(req) {
resp.response.write(`Hello, Fibjs!`);
});
server.start();
In the above example, we loaded a certificate and private key file named "server.crt" and "server.key", then created a service using the HttpsServer object, and started the service listening on port 8443. When the client When the client accesses the service through " https://localhost:8443/ ", it can receivesslEncrypted protection.
It should be noted that if external access is required, you need to ensure that the certificate is issued by a public trust agency, otherwise the client cannot verify it, reducing performance and security, and may trigger a security warning.
inheritance relationship
Constructor
HttpsServer
HttpsServer constructor, listen on all local addresses
1
2
3new HttpsServer(Array certs,
Integer port,
Handler hdlr);
Call parameters:
- certs: Array, list of server certificates
- port: Integer, specifiedhttpServer listening port
- hdlr:Handler,httpBuilt-in message processor, processing function, chain processing array, routing object, see for details
The format of certs is:
1
2
3
4
5
6
7
8
9
10
11[{
name: "fibjs.org",
crt: [X509Cert object],
key: [PKey object]
},
{
name: "*.fibjs.org",
crt: [X509Cert object],
key: [PKey object]
}
]
HttpsServer constructor
1
2
3
4new HttpsServer(Array certs,
String addr,
Integer port,
Handler hdlr);
Call parameters:
- certs: Array, list of server certificates
- 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 details
The format of certs is:
1
2
3
4
5
6
7
8
9
10
11[{
name: "fibjs.org",
crt: [X509Cert object],
key: [PKey object]
},
{
name: "*.fibjs.org",
crt: [X509Cert object],
key: [PKey object]
}
]
HttpsServer constructor, listen on all local addresses
1
2
3
4new HttpsServer(X509Cert crt,
PKey key,
Integer port,
Handler hdlr);
Call parameters:
- crt:X509Cert,X509CertCertificate, used by clients to authenticate servers
- key:PKey,PKeyPrivate key, used to talk to the client
- port: Integer, specifiedhttpServer listening port
- hdlr:Handler,httpBuilt-in message processor, processing function, chain processing array, routing object, see for details
HttpsServer constructor
1
2
3
4
5new HttpsServer(X509Cert crt,
PKey key,
String addr,
Integer port,
Handler hdlr);
Call parameters:
- crt:X509Cert,X509CertCertificate, used by clients to authenticate servers
- key:PKey,PKeyPrivate key, used to talk to the client
- 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 details
member properties
verification
Integer, set the certificate verification mode, the default is VERIFY_NONE
1Integer HttpsServer.verification;
ca
X509Cert, client certificate verification ca
1readonly X509Cert HttpsServer.ca;
maxHeadersCount
Integer, query and set the maximum number of request headers, the default is 128
1Integer HttpsServer.maxHeadersCount;
maxHeaderSize
Integer, query and set the maximum request header length, the default is 8192
1Integer HttpsServer.maxHeaderSize;
maxBodySize
Integer, query and set the maximum body size in MB, the default is 64
1Integer HttpsServer.maxBodySize;
enableEncoding
Boolean, automatic decompression function switch, turned off by default
1Boolean HttpsServer.enableEncoding;
serverName
String, query and set the server name, the default is: fibjs/0.x.0
1String HttpsServer.serverName;
socket
Socket, the server is currently listening toSocketobject
1readonly Socket HttpsServer.socket;
handler
Handler, the current event processing interface object of the server
1Handler HttpsServer.handler;
member function
enableCrossOrigin
Allow cross-domain requests
1HttpsServer.enableCrossOrigin(String allowHeaders = "Content-Type");
Call parameters:
- allowHeaders: String, specified to be acceptedhttpheader field
start
Start current server
1HttpsServer.start();
stop
Close the socket to abort the running server
1HttpsServer.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.
1String HttpsServer.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.
1Value HttpsServer.toJSON(String key = "");
Call parameters:
- key: String, not used
Return results:
- Value, returns a value containing JSON serializable