Sunday, June 23, 2024

Setting Up a Job Queue in Node.js with BullMQ and Redis

 https://docs.bullmq.io/

https://medium.com/@mjdrehman/setting-up-a-job-queue-in-node-js-with-bullmq-and-redis-in-5-minutes-0f170928c0b5

Sunday, June 16, 2024

Node.js Design patterns

 https://medium.com/@debiprasaddash_35810/node-js-design-patterns-8969d9184e37

Saturday, July 9, 2022

10 Powerful React Tools That You Should Know in 2022

Strorybook is something to try out. 

https://storybook.js.org/
https://javascript.plainenglish.io/10-powerful-react-tools-that-you-should-know-in-2022-76efa7fa711d

useMemo and useCallback in React

 https://medium.com/@jakobclausen/usememo-and-usecallback-in-react-913d307eb69

Friday, May 20, 2022

PM2 run different node version

To run several versions at the same time. In pm2, you can use the --interpreter options and specify the path to the node version you want.

You may install different node versions using nvm. 
Install NodeJS using Version Manager

Running direct script using PM2 process file:

module.exports = {
    apps : [{
      name   : "app1",
      cwd    : "/home/user/app1", // folder store the application
      script : "./app.js", // starting js file
      interpreter : 'node@6.9.1'    // Node version
    },
   {
      name   : "app2",
      cwd    : "/home/user/app2",
      script : "./app.js",
      interpreter : 'node@12.9.1'
   }]
}


Running using npm script using PM2 process file:

module.exports = {
    apps : [{
      name   : "app1",
      cwd    : "/home/user/app1", // folder store the application
      script : "/home/user/.nvm/versions/node/v6.9.1/bin/npm", // Folder where the npm stored
      args       : ["run", "start"],  // Argument pass to npm
      interpreter : 'node@6.9.1'    // Node version
    },
    {
       name   : "app2",
       cwd    : "/home/user/app2",
       script : "/home/user/.nvm/versions/node/v12.9.1/bin/npm",
       args       : ["run", "start"],
       interpreter : 'node@12.9.1'
    }]
}


Wednesday, January 12, 2022

TS-Node: Typescript execute engine

In my previous article, I have described how to create a simple typescript nodejs project. The method works fine in a small project, but take longer compilation time on a large project because its need to compile all typescript files on every change.
Create Node.js & Express.js with Typescript Project

The solution is using ts-node where it direct run the typescript without compiling it to javascript.

  1. Follow the steps to create a Node.js & Express.js with Typescript.
  2. Type "npm install -g ts-node".
  3. Add an entry to package.json.
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "build": "tsc -b",
        "start": "node dist/app.js",
        "dev": "nodemon dist/app.js",
        "dev:ts": "nodemon --exec ts-node src/app.ts"
      },
  4. Now you can just run the development using 1 console: npm run dev:ts

Saturday, July 24, 2021

New in Mongoose 5.10: Optimistic Concurrency - Safe Save Conflict

Optimistic concurrency is a better way to handle save conflict compare to the article I wrote previously.

https://scalaoncloud.blogspot.com/2021/03/how-to-avoid-save-conflict-in-mongoose.html


Optimistic concurrency tracks the version of the document and increments the version every time save(). If the version of the document in the database changed between when you loaded the document and when you save(), Mongoose will throw an error.

https://mongoosejs.com/docs/guide.html#optimisticConcurrency

https://thecodebarbarian.com/whats-new-in-mongoose-5-10-optimistic-concurrency.html 

Thursday, April 22, 2021

Create Node.js & Express.js with Typescript Project

    Here are the steps to create Node.js & Express.js with Typescript Project.

    1. Create a folder.
    2. Type command "npm init" to create package files.
    3. Type "npm install -g typescript" to install typescript.
    4. Type "tsc --init" to create typescript config file.
    5. Type "npm install express --save" to download express module.
    6. Type "npm install nodemon --save-dev" to download nodemon developer tool.
    7. Type "install install @types/node --save-dev" to download typescript node definition files.
    8. Type "install install @types/express --save-dev" to download typescript express definition files.
    9. Edit the package.json
    10. Change to main to "dist/app.js" to point to the start javascript file.
    11. Add the start scripts, "start" : "nodemon dist/app.js"
    12. The package.json will look like below:
    13. Edit the tsconfig.json
    14. Add "moduleResolution" : "node" under "module" : "commonjs".
    15. Change the "outDir" : "./dist". This is the location where typescript compiles the javascript.
    16. Change the "rootDir" : "./srv". This is where we store the typescript files.
    17. The tsconfig.json will look like below:
    18. Create /src folder and add app.ts. Edit the app.ts with the below script.
      import express from "express";

      const app = express();
      const port = process.env.port || 3001;

      // Adding middleway to use syntax req.body.name.
      app.use(express.json());

      app.get("/", (reqres=> res.send("Hello World!"));

      app.listen(port, () =>
        console.log(`Example app is listening on post ${port}!`)
      );

    19. Open a terminal run "npm start". This will start the web server using nodemon.
    20. Open another terminal run "tsc --watch". This will watch the /src folder to compile the ts to javascript.
    21. Call http://localhost:3001/ using postman. If you see "Hello World!", congratulation, you have completed the setup Node.js & Express.js with the Typescript project.

    Below is the project you may download from GitHub.

    Below GitHub is the example of the Node.js & Express.js project. You may compare the different of project setup.