loti v0.1.5
Lotijs
Overview
Loti js is an express framewrok to help you build express app easier, Loti uses Modules, Controllers, ressources and middlewares
// App.js
import * as Loti from "loti";
const App = new Loti.App();
// initialize the app and set up modules
App.init();
// listen for requests
App.router.listen(3000);
NOTE: the router property is the actual express app;
To create a Module you add
class Module extends Loti.LotiModule {
controllers = [["routeName", Controller]]; // The controllers are in a tuple [routeName, controller]
ressources = [["ressourceName", ressource]]; // just like in here
middlewares = [["middlewareName", middleware]]; // MiddlewareClass and now can be used in all controllers in module
name = 'moduleName' // Used for imports from other modules
exports = {
middlewares = ['middlewareName'],
ressources = ['ressourceName'] // Exporting some of our middlewares and ressources
}
imports = ['otherModule']
}
NOTE: Middlewares. ressources and controllers all have to be constructors NOTE: Models are also services
To add modules you just add
App.addModule(moduleName);
To create a controller you just type in
class Controller extends Loti.LotiController {
// Controller middlewares used on all routes in the controller
middlewares = [
"middlewareName", // Named in the module and imported middlewares can also be used
(req, res, next) => next(), // Can also use functions
MiddlewareClass, // And classes
];
ressourceNames = ["ressourceName"]; // Ressources from the module or imported ones
routes = [
[
"get", // The method can also be post, put, patch, delete, all and options
"home", // The route
"otherMiddleware", // You can still add other middlewares and handlers and classes
"**", // Mention the default controller middlewares: by default it is will always be at the start without the "**"
"index", // the method reference
],
];
index(req, res) {
res.send("hello world");
}
}
To remove the default middlewares from the controller you just add a *** any where in the route array
// Controller
routes = [["get", "", "***", "handlerName"]];
NOTE: adding ** and *** at the same time will add the default middlewares no matter the position of *** in the array
To create a class middlewares you type in
class Middleware extends Loti.Middleware {
// Handler has to be named index
index(req, res, next) {
console.log("middleware");
next();
}
}
To use it you can declare it in the module (the recommended way), insert it in a controller middlewares array or in a specific route array.
To add a service for exemple a mongoose Model service since the service has to be a contructor you add
class Auth extends Loti.LotiRessource {
private schema = {
name: String,
password: String
};
private model = mongoose.model('User', this.schema);
async signToken (user) {
return jwt(user); // sign the token
}
async login(userDto) {
try {
const newUser = await this.model.create(userDto);
const token = signToken(user);
return [token, null] // null because there are no errors
} catch(err) {
return [null, err]
}
}
}
and now we can use it in a controller like this
// ^^^ Controller code ^^^
constructor(_module, importedModules) {
super(_module, importedModules); // Modules and importedModules has to be passed on to a ressourceConsummer class like so
this.UserService = this.getRessource('ressourceName'
/* The ressource name is in the module or imported ones and refered in the controller resources array */
);
}
!!! DOCS NOT FINISHED !!!