SonicX
SonicX is open-source high performance lightweight router build upon core nodejs HTTP server.
- simple JSON based routing.
- less dependent on third party packages.
- developer-friendly.
- focus on high performance.
And of course, SonicX itself is open source with a public repository on GitHub.
Features!
- robust routing.
- middleware support.
- multipart form data support ( file uploads without third party library ).
- support dynamic routes creation ( /URL/:iUserId => access route params from req.params.iUserId ).
- static file serving.
- secure transport layer via https.
Installation
SonicX requires Node.js v8+ to run.
$ npm i sonicx --save
Usage
const sonicx = require;sonicx.route
Examples
API
Methods | Description |
---|---|
sonicx.listen(PORT, [, callback ]) | Starts the HTTPS server listening for encrypted connections. This method call http.createServer(config, handler).listen() method internally. |
sonicx.secureListen(PORT, config, [, callback ]) | Does same as sonicx.listen but it includes config params for ssl support. This method call https.createServer(config, handler).listen() method internally. |
sonicx.server | It is a reference of http.createServer(...) OR https.createServer(...) depends upon what are listening. |
sonicx.configuration | Holds properties to configure the global server behavior. |
sonicx.route(rootPath, [ Routes, ... ]) | Holds routes to be called defined path. |
.server
Reference of http server. can be used to assign as http engine to the third party libraries such as socket.io
Eg.
const sonicx = require;const server = const io = require;
Global Configuration [ optional ]
sonicx.configuration =
.route(rootPath, [ Routes, ... ])
-
rootPath: First parameter of route method, which get concat with each path property of routes defined in array.
-
Routes: This is a JSON Object Holds route properties.
- path [ Optional ]: get concat with rootPath as postfix and generate a full path.
- Eg.sonicx.route
- Eg.
- method [ Optional ]: GET, POST, PUT, DELETE is supported. ( case insensitive ).
- configuration [ Optional ]: Route level configuration will override the global configuration and has the same properties as the global configuration does excluding
staticPath
because it must be a global configuration. - middleware(req, res, next) [ Optional ]: has access to req, res and next. the controller will be executed only after calling the
next
method if the middleware is defined. - controller(req, res) [ Required ]: has access to req, res and responsible for writing API logic.
Eg.
sonicx.route - path [ Optional ]: get concat with rootPath as postfix and generate a full path.
Router Builder
-
Feature to add nested routes as parameter.
Eg.
const routeBuilder = newrouteBuilder.route
Request and Response parameter
Request and Response parameter has all property of nodejs HTTP server handler does. http server.
Request parameter has some other properties added such as.
- req.body : Contains JSON object sent in body of request from front-end.
- req.params : Contains JSON object fetched from dynamic url path.
sonicx.route('/user', [ { path : '/:iUserId', controller: (req, res) => { res.send({ iUserId: req.params.iUserId" } ) }, }, ]); // Request url : http://server_ip:server_port/user/1234 req.params = { iUserId: 1234 };
- req.query : Contains JSON object sent in query params in url.
Eg.
// Request url : http://server_ip:server_port/user/1234?name='shiva'&age=23 req.quey = { name: "shiva", age: 23 }
- req.files : Holds uploaded files data when requesting from
multipart/form-data
.- filename : holds name of the file
- buffer : buffer of file [ Only when
memoryUpload
is true ] - length : length of buffer in bytes [ Only when
memoryUpload
is true ] - path : path where files is stored [ Only when
memoryUpload
is false ]
Response parameter has some other properties added such as
// res.send(response:JSON, code:httpStatusCode [Optional], headers:Objects[Optional]);
res.send({ key: "It is SonicX" }, 200, { "Authorization" : "Bearer Token"});
License
ISC
Free Software, Hell Yeah!