nd-srv v1.0.9
nd-srv
Simply library to start the web server
It includes the basic functionality of a standard http server: routing, validation of input data, processing http headers and working with cookies.
Srv
To create a server, you need to create an instance of this class:
new Srv(option)
optionbasic server setuprouterinstance of class Routercontexta reference to the Сontext class or expanding it
const server = new Srv({
router: mainRouter,
context: Client,
})Every time a request is received, will be created instatnce of Context
server.request(cb)
cbFunctionreqIncomingMessage
Mirror for displaying incoming requests for additional analysis or logging
server.request(req => {
console.log(req)
});server.response(cb)
cbFunctionerrErrorresServerResponsedataAny
Triggered when the server has processed the request, or returns an error during processing
server.response((err, res, data) => {
if (err) {
console.log(err)
res.end(err.message)
return;
}
res.end(data)
});server.start(port, cd)
portnumbercbFunction
server.start(8080, () => {
console.log(`Server started on ${PORT}`);
})Router
Class for creating an object with routes.
Supports routing by the main types of requests:
GET | HEAD | POST | PUT | DELETE | CONNECT | OPTIONS | TRACE | PATH
new Router(routs)
routsObject
Example of a routs object:
export const mainRouter = new Router({
'/api': {
router: {
'/mark': {
'/': {
method: 'GET', // set HTTP method
async preHandler(client: Context) {
// check access or set headers
},
async handler(data: any): Promise<any> {
// do something
return data;
},
async postHandler(client: Context) {
// set headers or delet
},
},
router: {
'/create': {
method: 'POST',
preHandler: (client: Context): void => {},
handler: (data: any): any => {},
postHandler: (client: Context): void => {},
},
}
}
}
},
'/static': {
method: 'GET',
preHandler: (client: Context): void => {},
handler: (data: any): any => {},
postHandler: (client: Context): void => {},
}
});The router object consists of two things - it is the endpoint:
'/create': {
method: 'POST',
schema: {
name: {
type: 'string',
require: true,
reference: {
length: {
min: 3,
max: 15,
}
}
}
},
preHandler: async (client: Context): Promise<void> => {
},
handler: async (data: any, req: ReqHandler): Promise<any> => {
return 'hello world'
},
postHandler: async (client: Context): Promise<void> => {
},
}methodhttp methodschemathe validation scheme of the request body is described belowpreHandlerthe handler that is executed first (if specified). Has access to the context. Should be used to check access and set headershandlerthe main handler that is executed next. Has access to schema-validated (if specified) datadataAnyreqObjectqueryObject request paramcookieObject cookieheadersObject http headers
postHandlerthe last handler (if specified). Has access to the request context. Should be used to install or remove headers
Context
The class based on which the object will be created when the request is received.
context.pathname
pathnamestring
The main route of the request
context.host
hoststring
context.query
queryString
context.body
bodyObject
Request body. If there is a validation scheme, the validated data is placed here.
context.headers
headersObject
context.cookie
cookieObject
new Context(req, body)
reqIncomingMessagebodyObject
When a request is received, a class object is created with the request object and the request body.
context.setCookie(name, value, option)
nameString - name of cookievalueString - value of cookieoptionObjectexpiresStringmaxAgeStringpathStringhttpOnlyBoolean
context.setHeader(name, value)
nameString - name of headervalueString - value of header
conetext.delCookie(name)
nameString - name of cookie
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago