GithubHelp home page GithubHelp logo

deep-dey / bmx-onepiece Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bloomscorp/bmx-onepiece

1.0 0.0 0.0 54 KB

A starter template for node.js & mongoDB backend

TypeScript 99.48% JavaScript 0.52%

bmx-onepiece's Introduction

bmx-onepiece

The Express.js Starter Template is a pre-built project structure that gives developers a head start in building web applications using the Express.js framework. It provides a foundation for developers to build their applications upon, with a set of pre-configured files and folders that are commonly used in Express.js applications.

The template includes basic features such as routing, middleware, error handling, and database integration. It also includes a basic user authentication system and a sample database schema to help developers get started quickly.

Using the Express.js Starter Template can save developers a significant amount of time and effort in setting up a new project. It provides a solid foundation for building scalable and maintainable web applications.

To use the Express.js Starter Template, developers can simply clone the repository, install the required dependencies, and start building their application on top of the existing structure. The template is easily customizable and can be modified to suit the specific needs of a project.

Step: 1

Clone the repository and run npm -i to install dependencies.

Step2:

Create .env file in the root level and set the following environmental variables

DB_URI = mongodb+srv://username:password@db-url/database-name
PORT = 3000
NVERSE_AES_KEY = 32 character secret key
NVERSE_AES_IV = 32 character secret key
NVERSE_PASSWORD_KEY = 16 character secret key
NVERSE_JWT_SECRET = 1024 character secret key
MULTER_PATH = path to store temp files
SERVER_URL = http://127.0.0.1:3000

you can generate these keys randomly as follows

// NVERSE_AES_KEY
node -e "console.log(require('crypto').randomBytes(16).toString('hex'))"
// NVERSE_AES_IV
node -e "console.log(require('crypto').randomBytes(16).toString('hex'))"
// NVERSE_PASSWORD_KEY
node -e "console.log(require('crypto').randomBytes(8).toString('hex'))"
// NVERSE_JWT_SECRET
node -e "console.log(require('crypto').randomBytes(512).toString('hex'))"

Step 3:

To run the app open two terminals

In the first terminal build and watch the application for changes (as in package.json)

npm run build-watch

In the second terminal run the build application (as in package.json)

npm run start-dev

The above command needs nodemon installed globally

npm install -g nodemon

Run with multiple instances

Node.js is built on a single-threaded event loop, which means that it can only handle one task at a time. This can be a problem when dealing with tasks that are CPU-intensive or take a long time to complete, such as processing large amounts of data or handling complex calculations.

When a task is running in Node.js, it blocks the event loop, which means that other tasks have to wait until the current task is finished before they can run. This can lead to slow performance and can cause the server to become unresponsive.

One solution is to use a cluster module to enable multi-threading in Node.js. The cluster module allows you to create multiple instances of your application and distribute the workload across them. This can improve performance and make your application more scalable.

To do so we use pm2 package.

To install pm2 package globally run: npm install pm2 -g

Then install to handle logs: pm2 install pm2-logrotate

Create ecosystem.config.cjs in your root directory.

๐Ÿ’ก To know more about configs see documentation

module.exports = {
	apps: [
		{
			name: "app-name",
			exec_mode: "cluster",
			instances: "3",
			script: "./dist/server.js", // your script
			args: "start",
			watch: false, // only for development
			maxRetries: 2,
			log_date_format: "YYYY-MM-DD HH:mm:ss",
			time: true,
			combine_logs: true,
			out_file: "./logs/out.log", // current out logs
			error_file: "./logs/errors.log", // current error logs
			env: {
				DB_URI: "mongodb+srv://username:password@db-url/database-name",
				PORT: 3000,
				NVERSE_AES_KEY: "32 character secret key",
				NVERSE_AES_IV: "32 character secret key",
				NVERSE_PASSWORD_KEY: "16 character secret key",
				NVERSE_JWT_SECRET: "1024 character secret key",
				MULTER_PATH: "path to store temp files",
				SERVER_URL: "http://127.0.0.1:3000"
			},
		},
	],
};

In pm2 logs can be very big to handle logs and delete old logs we set some log configs,

๐Ÿ’ก To know more see documentation

pm2 set pm2-logrotate:retain 30
pm2 set pm2-logrotate:compress false
pm2 set pm2-logrotate:dateFormat YYYY-MM-DD_HH-mm-ss
pm2 set pm2-logrotate:max_size 10M
pm2 set pm2-logrotate:rotateInterval 0 0 * * *
pm2 set pm2-logrotate:rotateModule true
pm2 set pm2-logrotate:workerInterval 120

๐Ÿ’ก To know more about pm2 logs see documentation

PM2 Commands

  • To start an application: pm2 start ecosystem.config.cjs
  • To list all the running instances: pm2 list
  • To delete clusters of a particular application: pm2 delete application-name
  • To delete all clusters and applications: pm2 delete all
  • To monitor resources usage: pm2 monit
  • To display all logs: pm2 logs
  • To display specific application logs: pm2 logs application-name
  • To display last 100 lines of logs: pm2 logs --lines 100
  • To display only error logs: pm2 logs --err
  • To display only error logs: pm2 logs --out
  • To start a pm2 process: pm2 start pid
  • To stop a process: pm2 stop pid

bmx-onepiece's People

Contributors

deep-dey avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.