mobrix-backend
v1.1.0
Published
An intuitive Express.js wrapper, to easily build an expressjs backend with a simplified configuration
Downloads
15
Maintainers
Readme
MoBrix-backend
An intuitive Express.js wrapper, to easily build an expressjs backend with a simplified configuration
Summary
Check out the official MoBrix-backend guide page for more details
Getting started
Installation
If you want to use this library inside your project, just install it:
npm i mobrix-backend
Usage
Once installed, you can import this library in your project and use it:
Basic example
This is a basic example, with just a standard MoBrix-backend init:
import { startMbxBackend } from "mobrix-backend";
startMbxBackend();
We can see the default page by navigating to localhost:3000
:
Advanced example
This example inits and starts a MoBrix-backend instance using the configuration parameters. It will listens at port 3000, with 3 GET handlers, a custom callback and a router:
import { Request, Response, Router, startMbxBackend } from "mobrix-backend";
import dotenv from "dotenv";
dotenv.config();
const port = process.env.PORT || 3000;
const router = Router();
const timeLog = (req: any, res: any, next: () => void) => {
console.log("Time: ", Date.now());
next();
};
router.get("/", (req, res) => {
res.send("router 1 - Main");
});
router.get("/router1-r1", (req, res) => {
res.send("router 1 - Route 1");
});
startMbxBackend({
callback: (app) => {
app.use(timeLog);
},
port: Number(port),
onListen: () => {
console.log(`[server]: Server is running at http://localhost:${port}`);
},
routers: [{ path: "/router1", router }],
get: [
{
path: "/",
callback: (req: Request, res: Response) => {
res.send("MoBrix-backend Server");
},
},
{
path: "/r1",
callback: (req: Request, res: Response) => {
res.send("Main path - Route 1");
},
},
{
path: "/r2",
callback: (req: Request, res: Response) => {
res.send("Main path - Route 2");
},
},
],
});
Configuration parameters
| Parameter | Description | Default value |
| ------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | ------------- |
| callback | Custom function to interact directly with internal Expressjs app | / |
| get | Custom Expressjs app get
handlers array. Every handler is composed by a path and a callback function | [] |
| middlewares | Custom middlewares function loaded into the Express app | [] |
| onListen | Custom function called everytime the backend app is listening for incoming requests | / |
| port | Custom Expressjs app listening port | 3000 |
| post | Custom Expressjs app post
handlers array. Every handler is composed by a path and a callback function | [] |
| routers | Custom Expressjs app routers
array. Every handler is composed by a path and a Router | [] |
Tests
Unit tests are located inside tests
folder. The test script is executed with pre-defined test command:
npm run test
Authors
License
This project is licensed under the MIT License - see the LICENSE file for details