Code Monkey home page Code Monkey logo

backend-setup-node's Introduction

Node.js Typescript Modern Starter

How to setup a production level node project for backend While developing with Typescript and Node.js is awesome, setting up a new project is painful. This minimal and modern starter repo is here to help you get started with Node.js and Typecript without the pain.

components/ goals

  1. setup basic TS and node boiler plate
  2. Api server setup with graceful shutdown
  3. Api versioning setup
  4. CORS handling
  5. mysql db connection using sequelize ORM
  6. Mongo db connection using mongoose
  7. DAL layer for interacting with mysql or mongoose or any other db related functions even redis
  8. Redis setup
  9. Swagger setup
  10. Test case setup using jest
  11. Logger for different levels - info, error, warning, debug using winston
  12. Sentry for error alerting and profiling / New Relic

Overview

This starter uses a bare-bones and minimal approach to get anyone up and running with a new project in no time. It provides:

  • Typescript 5 with a strict tsconfig.
  • Yarn/Npm scripts ready to do everything you commonly need. Supporting compile, clean, test, bundle, dev and start. These scripts are created to be compatible with the operating systems linux, macos and windows.
  • Github Actions in place runnung with current node LTS version (20) on linux, macos and windows to automatically (for each PR):
    • test the code
    • compile the codebase from ts to js
    • check for formatting issues
    • check for linting issues
  • Testing via jest
  • Formatting via prettier.
  • Linting via eslint and typescript-eslint
  • Bundling via esbuild, a fast bundler that "just works".
  • Debugging set up with examples for vscode and vim.
  • Automated dependency updates via renovate.
  • Using the current LTS, nodejs 20

Prerequisites

Quickstart

  • Clone the repo git clone [email protected]:nihalpandey4/backend-setup-node
  • Remove the .git folder cd backend-setup-node && rm -rf .git
  • (optional) Update the package.json name, author, keywords, etc..
  • Set up your own git folder and create your first commit. Run git init && git add . && git commit -am "initial commit"
  • (optional) Set up the git hook for formatting your code. cp .git-hooks/pre-commit .git/hooks/pre-commit. For windows you need to use WSL to use this.
  • Use the node version specified in .nvmrc nvm install && nvm use (on windows you need to specify the node version in the command)
  • Enable corepack and update yarn corepack enable
  • Install dependencies yarn
  • You're done🎉 What about you try running the tests? Run yarn test. See the section below for all available commands together with their explanation.

Scripts and their explanation

All scripts can be found inside the package.json file under the "scripts" attribute.

  • yarn bundle -> Bundles the whole code into a single javascript file which will be stored inside the dist folder. For prod deployments you typically just copy this file somewhere and then run something like node --enable-source-maps ./index.js.
  • yarn clean -> Removes bundled files by deleting the dist folder. Normally there is no need to invoke this manually.
  • yarn compile -> Runs the typescript compiler against the typescript codebase. Displays any errors if they occur.
  • yarn compile:watch -> Runs the typescript compiler every time you make changes to a file. It is good to open this in another terminal while developing to spot typescript issues.
  • yarn dev -> This should be used for running the code while developing. It watches all changes you make to your typescript codebase and automatically rebuilds the project. It does also watch all changes made to the built project and restarts the code whenever changes are detected. This enables a quick feedback loop.
  • yarn debug -> Starts the app in debugging mode. Waits for a debugger to attach. See Debugging below for more info.
    • If you want to restart the debugging process every time you change the code, you can use something like nodemon --watch src --watch test --ext ts,json --exec 'yarn debug' or when debugging tests with nodemon --watch src --watch test --ext ts,json --exec 'yarn debug:test'
  • yarn debug:test -> Starts the test run in debugging mode. Waits for a debugger to attach. See Debugging below for more info.
  • yarn format -> Formats the code using prettier.
  • yarn format:check -> Checks for formatting errors using prettier. This is typically only invoked by the CI/CD pipeline.
  • yarn lint -> Lints the code using eslint. Fixes problems that are auto-fixable and reports the rest of them to you.
  • yarn lint:check -> Checks for linting errors using eslint. This is typically only invoked by the CI/CD pipeline.
  • yarn start -> Runs the code. This only works if the code was bundled before ;).
  • yarn test -> Tests your codebase. Basic tests are created for both major common approaches of putting tests beside the source code as well as putting tests in a separate folder.
    • You can inspect the code coverage in depth by running npx http-server ./coverage/lcov-report and then browsing http://localhost:8080.

Debugging

An enourmous amount of people default to console log debugging since understanding the setup for debugging typescript can be somewhat awful and painful. This repo provides a debug config and guide ready to use for vscode and for vim using vimspector. Both use the mostly DAP compliant debugger vscode-js-debug.

Debugging Code

There are somewhat "different" ways of starting the debugger. Once is by starting the app and waiting for a debugger to connect and the other one is starting the app initiated by the debugger. I made the experience that the former works on any given code base, no matter the amount of transipilation or bundling steps and custom steups while the latter does fail in extremely customized scenarios. Therefore here only the first one is covered with examples.

Vim or Vscode

  • Run yarn debug in another terminal
  • Open src/index.ts vim ./src/index.ts (or code ./src/index.ts) in another terminal.
  • Set breakpoint somewhere in the file at the console log (F9 is the default mapping).
  • Start by pressing F5
  • Press F5 again, should see the console.log output
  • Done🎉

Debugging Tests

Vim or Vscode

  • Run yarn debug:test in another terminal
  • Open src/index.ts vim ./src/hello.test.ts in another terminal.
  • Set breakpoint in the line of the console log in the test file.
  • Start by pressing F5 (then skip the jest internal file once with F5)
  • Check the terminal where you ran yarn debug:test, it should not display the console log yet.
  • Press F5 again, should see the console.log output there now.
  • Done🎉

Linting

This repo has eslint and typescript-eslint as well as an automated Github Action to check for linting set up and ready to go.

The rules in this project are my personal preference and reflect a subset of the recommended options. They also include a lot of the more strict options (NOT included in the recommended ones). My goal is to simplify having a consistent code base/code style, to avoid catchable bugs early and advocate for usage of newer features of the language.

However, I made it dead simple to enable the default/recommended eslint rules, if you want to use them instead. Everything is documented, just browse to ./.eslintrc.cjs and adapt the code.

Automated Dependency Updates

After using this repo (either via the github template or by simply cloning it) you have to set up a renovate bot. For github this can easily be done via the Github Apps renovate as well as renovate-approve. To be able to the mimic the approach used in this repo, you should set up:

  • the repo setting to allow auto-merge
  • a branch protection rule for the main branch to require approval (will be handled via renovate-approve)

Folder structure followed inside src/

  • src -- index.ts ---- routes -------- ---------- index.ts ------------ -------------- index.ts -------------- ---- <services/helpers> -------- <entity.ts> (will have multiple functions named exports) ---- databases ------ models -------- index.js -------- <entity_name.model.js> ------ services ---- utilities ------ logger ---- config ------ index.js ---- middlewares

backend-setup-node's People

Contributors

yashpandeyfarmart27 avatar nihalpandey4 avatar

Watchers

 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.