(FRONT) FRONT (2024)

Common Node.js backend logic (plus some of my YUP filters)

To be honest, if I would start NodeBackend project from scratch I prefer Nest.JS, something like that project 12. Server Angular (NestJs) project., but in this case I working as part of team, therefore I have no choice. So, how need to build NodeBackend project without additional frameworks.

Firstly, need to initialize Node config correctly and install needed packages:



This is workflow to initialize folders as Node.JS backend. About Git you can read this post Change Git account


# npm init
# git init
# npx gitignore node
# npm install XXX
# npm run start-dev

Pay attention that by default we develop in "type": "commonjs" mode, alternative development is "type: module". JS ES modules has different syntax for attach functions and main point that ES modules loading asynchronous, but CommonJS loads modules synchronously. We can see "waterfowl" in browser with ES modules, but CommonJS modules we can pack in production mode with WebPack. This two Node.JS development mode has different syntax, and for CommonJS we can include function in any place of code.


// For CommonJS 
const myFunc = require('./utils/my-module')
// For ES6 Module 
import myFunc from './utils/my-module'
import {add, subtract} from './utils/my-module'

Secondary need to install needed packages, from Nodemon https://www.npmjs.com/package/nodemon (what allow reload backend if JS code changed) and need to install .Env package https://www.npmjs.com/package/dotenv, because Node backend usually used a lot of variables:



Main listener what will be listen request to Backend. In my case listener looks as:



This function call Express router. Than usually we need to create Route.JS module what implement Express router.



Router module contains a routing - what controller will processing request and what authorization we need and what YUP filter will be applied to allow request to passthrough to backend.



As you can see, this module perform some Node middleware [1] functions with suffix DTO (that used YUP filters) and functions [2] apiKeyGuard, [3] rateLimit, [4] validate, [5] apiErrorHandler, [6] validateJwt. And finally pass processing request to specific endpoint into [7] controllers.

  1. So, firstly about YUP filter https://www.npmjs.com/package/yup. This is hard parts of Node.JS workflow because usually you need some special implementation and direct debugging this functions is impossible. Its very rare situation if you enough standard YUP function, usually you need equalTo, array, decimal, timestamp, enum value and other value what not present in standard YOU datatype.


  2. apiKeyGuard is middleware functions what check additional header key, this is simplest way for protect Backend from rogue request


  3. Usually we need to turn off rateLimit function in debugging and testing process, this function need to activate in production mode.



    Of course we can use more carefully solution instead comment, something like this:


    var env = process.env.NODE_ENV || 'development';
    

    But a lot of other developers will worked after me in this project and this is not required from me in this project.

  4. validate is very important middleware function what checking AU JWT (if it required from parameters) and applying YUP filter.



  5. apiErrorHandler - this is dead end. If we faced with any error in validation process or in controllers. We need to connect this handler when Express started and can perform in controllers or any other places.


  6. validateJwt - is function what extract UserID from JWT and check various right of users - is user active, is user has admin right, is user banned for IP and so on.


  7. And finally controllers. I created a couple of controllers for this projects, for example Refactoring Boompfi/Strip gateway to subscription mode, but main my controller in this project was this controller.


  8. Pay attention, that controller by conventions is class, but of course, we can attach any common JS modules with set of separate functions.



That's it about common Node JS development workflow.

For manual debugging Backend we can use Best REST client for Node and VS.CODE and for automatic testing we can use https://jestjs.io/, but in this project automatic testing is not my responsibility.









Comments ( )
Link to this page: http://www.vb-net.com/NodeBackend/Index.htm
< THANKS ME>