- class
HTTPServer
- class
Router
- class
Request
- class
Response
- enum
RequestStatus
- enum
HTTPServerEvent
- enum
HTTPMethod
- enum
ContentType
- enum
HandlerType
- type
HTTPServerOptions
- type
Cookie
- type
CookieAttributes
- type
HandlerFunction
- type
Route
The port on which the server is listening.
The HTTPServer
class extends the Router
class, please see router properties for the inherited properties.
Creates a new HTTP server.
Parameter | Type | Description |
---|---|---|
options |
?HTTPServerOptions |
The options for the server |
const port = 3000;
const server = new HTTPServer({ port });
Adds an event listener on the server.
Parameter | Type | Description |
---|---|---|
event |
HTTPServerEvent |
The event to listen for |
callback |
function | The listener for the event |
Notes
Don't add listeners forHTTPServerEvent.Request
as this event is already handled internally.
Don't add listeners forHTTPServerEvent.Listening
, instead use the callback of<HTTPServer>.start()
.
Starts the server on the port specified in the server options (see <HTTPServer>.port
).
Parameter | Type | Description |
---|---|---|
listeningCallback |
function | Triggered when the server is ready |
server.start(() => {
console.log(`Server started at http://localhost:${port}`);
});
Closes the server.
The HTTPServer
class extends the Router
class, please see router methods for the inherited methods.
All the routers mounted on this router.
All the handlers and their specific path and method on this router. See Route
for more details.
The type of the Handler. It is always HandlerType.Router
. See HandlerType
for more details.
The path where the router is mounted.
The methods handled by this router. It is always HTTPMethod.Any
. See HTTPMethod
for more details.
Creates a new router.
Adds a new handler for GET
requests.
Parameter | Type | Description |
---|---|---|
path |
string | The path where to add the handler |
handler |
HandlerFunction |
The handler to add |
Adds a new handler for POST
requests.
Parameter | Type | Description |
---|---|---|
path |
string | The path where to add the handler |
handler |
HandlerFunction |
The handler to add |
Adds a new handler for PATCH
requests.
Parameter | Type | Description |
---|---|---|
path |
string | The path where to add the handler |
handler |
HandlerFunction |
The handler to add |
Adds a new handler for DELETE
requests.
Parameter | Type | Description |
---|---|---|
path |
string | The path where to add the handler |
handler |
HandlerFunction |
The handler to add |
Adds a new handler for any request method.
Parameter | Type | Description |
---|---|---|
path |
string | The path where to add the handler |
handler |
HandlerFunction | Router |
The handler to add |
<Request>.body
<Request>.headers
<Request>.method
<Request>.url
<Request>.params
<Request>.query
<Request>.data
<Request>.cookies
<Request>.timestamp
The body of the request. It is a string or a Buffer, depending on the Content-Type header.
The raw headers of the request. See https://nodejs.org/docs/latest/api/http.html#messageheaders for more details.
The http method of the request. See HTTPMethod
for all possible values.
The url of the request. See https://nodejs.org/docs/latest/api/url.html#class-url for more details.
The params in the request's path.
server.get('/users/:username', (req, res) => {
console.log(req.params.username);
return RequestStatus.Done;
});
Here we declare a route with a param (:username
). Whenever in the path of a handler there is a slash followed by a colon (/:
), it marks a param. There can be multiple params in the same handler path, but their name must be different. The corresponding part of the request's pathname will be the value of the param.
With the previous code, if the request's pathname is /users/jean
, req.params.username
will be jean
, and therefore jean
will be logged in the console.
The query/search-params of the request. See https://nodejs.org/docs/latest/api/url.html#class-urlsearchparams for more details.
This property is used to store any data you want to pass to the next handlers that will handle the request.
server.use('/*', (req, res) => {
req.data.authenticated = false; // default to false
if (isAuthValid(req.headers.authorization)) { // perform some auth validation
req.data.authenticated = true; // set the auth to true if the validation success
}
return RequestStatus.Next;
});
server.get('/protected', (req, res) => {
if (req.data.authenticated) { // if the validation successed
res.send('Welcome to the protected page');
} else { // otherwise the user is not allowed
res.status(401);
res.send('Invalid auth');
}
return RequestStatus.Done;
});
The cookies sent with the request.
The timestamp at which the server received the request.
<Response>.checkContentType
<Response>.headers
<Response>.headSent
<Response>.sent
<Response>.contentType
<Response>.code
<Response>.cookies
<Response>.sentTimestamp
Whether or not the content-type checking when sending data is enabled. Use <Response>.setContentTypeCheck()
to modify this value.
The currently set headers for this reponse. The Set-Cookie
headers are not included. To view the cookies, see <Response>.cookies
.
Whether or not the head of the response has been sent. If true, the headers and the status code cannot be changed.
Whether or not the body of the response has been sent. See <Response>.end()
for more details.
The content-type of the response's body. If no data has been send yet, the value is null
. See ContentType
for more details.
The status code of the response. By default it is 200
. Use <Response>.status()
to modify it.
The cookies that have been set on this response. See <Response>.setCookie()
for more details.
The timestamp when the <Response>.end()
method has been called. This property is null
before <Response>.end()
has been called. View <Response>.end()
for more details.
<Response>.setContentTypeCheck()
<Response>.send()
<Response>.setHeader()
<Response>.setCookie()
<Response>.status()
<Response>.end()
Reponse.onSent()
Enable or disable the content-type checking.
Parameter | Type | Description |
---|---|---|
value |
boolean | The new state of the content-type checking |
Send some data.
Parameter | Type | Description |
---|---|---|
data |
string | Buffer | The data to send |
Set or modify a header. Do not use this to set cookies, as the last set cookie will overwrite all the other previously set. To set cookies, use <Response>.setCookie()
.
Parameter | Type | Description |
---|---|---|
name |
string | The name of the header to set |
value |
string | The value of the header |
Set a cookie on the response.
Parameter | Type | Description |
---|---|---|
name |
string | The cookie's name |
value |
string | The cookie's value |
attributes |
?CookieAttributes |
The cookie's attributes |
Set the status code of the response. See <Response>.code
for more details.
Parameter | Type | Description |
---|---|---|
code |
number | The new status code |
End the response. Optionally send data, then set <Response>.sent
to true
.
Parameter | Type | Description |
---|---|---|
data |
?(string | Buffer) | The data to send before ending the response |
Adds a function to be called just after the request is sent. Useful for logging middlewares.
Parameter | Type | Description |
---|---|---|
callback |
function | The function to be called |
server.use('/*', function logger(req, res) {
res.onSent(() => {
console.log(`timestamp: ${res.sentTimestamp} | code: ${res.code}`);
});
return RequestStatus.Next;
});
The possible status for a request in the handling process, returned by the handler functions.
Member | Description |
---|---|
RequestStatus.Done |
The request has been handled and the response is sent |
RequestStatus.Next |
The handler has finished his job and passes the request to the next handler |
RequestStatus.Error |
An error occured during the handling process |
The different events that the server can encounter. See <HTTPServer>.on()
.
See the Node.js documentation for more details about each event.
Member | Description |
---|---|
HTTPServerEvent.CheckContinue |
- |
HTTPServerEvent.CheckExpectation |
- |
HTTPServerEvent.ClientError |
- |
HTTPServerEvent.Close |
- |
HTTPServerEvent.Connect |
- |
HTTPServerEvent.Connection |
- |
HTTPServerEvent.DropRequest |
- |
HTTPServerEvent.Error |
- |
HTTPServerEvent.Listening |
- |
HTTPServerEvent.Request |
- |
HTTPServerEvent.Upgrade |
- |
Notes
Don't add listeners forHTTPServerEvent.Request
as this event is already handled internally.
Don't add listeners forHTTPServerEvent.Listening
, instead use the callback of<HTTPServer>.start()
.
The different http methods handled.
Member | Description |
---|---|
HTTPMethod.Get |
- |
HTTPMethod.Post |
- |
HTTPMethod.Patch |
- |
HTTPMethod.Delete |
- |
HTTPMethod.Any |
Special value used when <Router>.use() is called |
The different content-types.
Member | Description |
---|---|
ContentType.Text |
Used for textual content (i.e. html) |
ContentType.JSON |
Used for JSON body |
ContentType.OctetStream |
Used for all other data types |
The two possible types of handler.
Member | Description |
---|---|
HandlerType.Router |
Used for Router |
HandlerType.RouterFunction |
Used for Route |
The options passed to new HTTPServer()
.
Property | Type | Description |
---|---|---|
HTTPServerOptions.httpServer |
?Server | A preexisting server object (from node:http ) |
HTTPServerOptions.port |
?number | The port on which the server listens (see <HTTPServer>.start() ) |
Represents a cookie value and attributes. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie for more details.
Property | Type | Description |
---|---|---|
Cookie.value |
string | The value of the cookie |
Cookie.attributes |
?CookieAttributes |
The attributes of the cookie |
Represents the attributes of a cookie. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#attributes for more details.
Property | Type | Description |
---|---|---|
CookieAttributes.secure |
?boolean | - |
CookieAttributes.maxAge |
?number | - |
CookieAttributes.httpOnly |
?boolean | - |
Functions used to handle requests.
Parameter | Type | Description |
---|---|---|
request |
Request |
The incoming request |
response |
Response |
The outgoing response |
Internal wrapper for HandlerFunction
so they have common internally required properties with Router
.
Property | Type | Description |
---|---|---|
Route.path |
string | the path of the route |
Route.method |
HTTPMethod |
the method this route handles |
Route.type |
HandlerType.RouterFunction |
the type of the route |
Route._handle |
HandlerFunction |
the wrapped handler function |