Sunday 23 February 2020

I'm beginner, is that a good choice to organize a project

Hi,I'm a beginner to nodejs, and i want to write a simple first API.I want to organize my projetc with a multi layered design. folders are like this : middlewares -> logger, authentification... repository -> call to the database routes -> all the routers services -> the business logic app.js -> entry of the appwhen an action is done I follow this schema :app.js -> routes -> services -> repositoryand I do this for exemple :router : ```javascript const service = require("../services/users");router.get("/", (req, res) => { service.getAll() .then(data => res.json(data)) .catch(err => res.send(err)); }); ```service : ```javascript const {repository} = require("../repository/users");const service = {getAll: async () => { // SOME BUSINESS LOGIC HERE try { return await repository.getAll() } catch (err) { throw new Error(err); } }};module.exports = service; ```repository : ```javascript const repository = {getAll: async () => { // CALL THE DATABASE AND PUT RESULT IN users[] if (users) { return users; } else { throw Error("can't recept users"); } } };module.exports = repository; ```My questions are : - Is it a good practice / common practice to multi-layered with nodejs ? - I don't want to use es6 classes at this time, I put some methods (getAll()...) in objects (repository, service...) and exports them, is it ok to do that like this ?Thanks for your response, and sorry for my english it's not my first language...

Submitted February 23, 2020 at 08:58PM by bloodgast

No comments:

Post a Comment