• Skip to main content

Rokturis

Pie kā turēties

  • Full Stack
    • JavaScript
    • JS Snipets
    • CSS
    • Git
    • VS Code

Nov 13 2023

Backend API

Requirements:

  • Node.js
  • npm
  • Express https://expressjs.com/en/starter/installing.html
  • mongoose
  • Nodemon
  • Studio 3T https://studio3t.com/
  • JWT

Layers (las capas):

  • api/router.js
  1. api/clients/clients.router.js
  2. api/clients/clients.controller.js
  3. api/clients/clients.service.js
  4. api/clients/clients.repository.js
  • api/clients/clients.model.js

req un res always stays in the controller.

return res.json // always with return, otherwise keeps executing the code that follows.

Import – Export

import { hola, adios } from './exportConst.js'; // importing functions as object.
import * as exportConst from './exportConst.js'; // importing all. * is naming the object.
import whatEverName from './exportDefault.js'; // when there is only one function. Possible changing functions name.

Controller

The best practice for controllers is to keep them free of business logic. Their single job is to get any relevant data from the req object and dispatch it to services. The returned value should be ready to be sent in a response using res.

Make sure you don’t pass any web layer objects (i.e. req, res, headers, etc) into your services. Instead, unwrap any values like URL parameters, header values, body data, etc before dispatching to the service layer.

params => obligatory

query => optional.

query received as string (true if exist)

JWT

Written by meudza · Categorized: Uncategorized

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recipe Rating




Copyright © 2026 · Altitude Pro on Genesis Framework · WordPress · Log in