• Stars
    star
    1,289
  • Rank 36,496 (Top 0.8 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 5 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

NestJS (Express + TypeORM + GraphQL + MongoDB) codebase containing real world examples (CRUD, auth, advanced patterns, etc).

Nestjs GraphQL Best Practice

NestJS (Express + Typeorm) codebase containing real world examples (CRUD, auth, advanced patterns, etc).

Build Status CircleCI Known Vulnerabilities Greenkeeper badge Coverage Status

Node.js Best Practices

Version 7.5.0

Table of Contents

Structure

Node.js Best Practices

Function

  1. Dynamic import
  2. Authenticate
    • Config jwt like OAuth ( access-token, refresh-token )
    • OAuth Google
    • OAuth Facebook
  3. Dump database
    • Child process
  4. Logger
    • NestJs
    • Wiston
  5. Send mail
    • Nodemailer
  6. Payment
    • Stripe
  7. Task scheduler
    • Timeout
    • Interval
    • Cron
  8. Translate
    • Google translate
  9. Upload file
    • Cloudinary
    • Fs createWriteStream to folder static
  10. Test
    • Unit
    • E2e
    • Coverage

Usage

  1. Clone repository
  git clone https://github.com/chnirt/nestjs-graphql-best-practice.git
  1. Cd into directory
  cd nestjs-graphql-best-practice/
  1. Create .env
  touch .env
  1. Add to .env
  PORT=<yourport>
  1. Install dependencies using npm
  npm i

Starting the Server

  1. Generate graphql.schema.ts
  npm run gen

2.1 Start in development normal

  npm run start:dev

2.2 Start with webpack ( 2 terminal view )

  npm run webpack
  npm run start:hmr

Buy Me A Coffee

Node.js Best Practices

Node.js Best Practices

1. Project Structure Practices

[โœ”๏ธ] 1.1 Structure your solution by components

[โœ”๏ธ] 1.2 Layer your components, keep Express within its boundaries

[โœ”๏ธ] 1.3 Wrap common utilities as npm packages

[โŒ] No neccessary - 1.4 Separate Express 'app' and 'server'

[โœ”๏ธ] 1.5 Use environment aware, secure and hierarchical config

2. Error Handling Practices

[โœ”๏ธ] 2.1 Use Async-Await or promises for async error handling

[โœ”๏ธ] 2.2 Use only the built-in Error object

![โ”] 2.3 Distinguish operational vs programmer errors

[โœ”๏ธ] 2.4 Handle errors centrally, not within an Express middleware

[โœ”๏ธ] 2.5 Document API errors using Swagger or GraphQL

[โœ”๏ธ] 2.6 Exit the process gracefully when a stranger comes to town

[โœ”๏ธ] 2.7 Use a mature logger to increase error visibility

[โœ”๏ธ๏ธ] use Jest - 2.8 Test error flows using your favorite test framework

![โ”] 2.9 Discover errors and downtime using APM products

[โœ”๏ธ] 2.10 Catch unhandled promise rejections

[โœ”๏ธ] 2.11 Fail fast, validate arguments using a dedicated library

3. Code Style Practices

[โŒ] No neccessary - 3.1 Use ESLint

[โ”] 3.2 Node.js specific plugins

[โœ”๏ธ] 3.3 Start a Codeblock's Curly Braces on the Same Line

[โœ”๏ธ] 3.4 Separate your statements properly

[โœ”๏ธ] 3.5 Name your functions

[โœ”๏ธ] 3.6 Use naming conventions for variables, constants, functions and classes

[โœ”๏ธ] 3.7 Prefer const over let. Ditch the var

[โœ”๏ธ] 3.8 Require modules first, not inside functions

[โœ”๏ธ] Nest must import files directly - 3.9 Require modules by folders, opposed to the files directly

[โœ”๏ธ] 3.10 Use the === operator

[โœ”๏ธ] 3.11 Use Async Await, avoid callbacks

[โœ”๏ธ] 3.12 Use arrow function expressions (=>)

4. Testing And Overall Quality Practices

[โœ”๏ธ] 4.1 At the very least, write API (component) testing

[โœ”๏ธ] use Jest - 4.2 Include 3 parts in each test name

[โœ”๏ธ] use Jest - 4.3 Structure tests by the AAA pattern

[โœ”๏ธ] 4.4 Detect code issues with a linter

[ใ€ฝ๏ธ] use Jest - 4.5 Avoid global test fixtures and seeds, add data per-test

[โœ”๏ธ] 4.6 Constantly inspect for vulnerable dependencies

![โ”] 4.7 Tag your tests

[โœ”๏ธ] 4.8 Check your test coverage, it helps to identify wrong test patterns

[โœ”๏ธ] 4.9 Inspect for outdated packages

[โœ”๏ธ] 4.10 Use production-like env for e2e testing

[โœ”๏ธ] 4.11 Refactor regularly using static analysis tools

[โœ”๏ธ] 4.12 Carefully choose your CI platform (Jenkins vs CircleCI vs Travis vs Rest of the world)

5. Going To Production Practices

![โ”] 5.1. Monitoring!

[โœ”๏ธ] 5.2. Increase transparency using smart logging

![โ”] 5.3. Delegate anything possible (e.g. gzip, SSL) to a reverse proxy

[โœ”๏ธ] 5.4. Lock dependencies

![โ”] 5.5. Guard process uptime using the right tool

[โœ”๏ธ] 5.6. Utilize all CPU cores

[โœ”๏ธ] 5.7. Create a โ€˜maintenance endpointโ€™

[โœ”๏ธ] 5.8. Discover errors and downtime using APM products

[โœ”๏ธ] 5.9. Make your code production-ready

![โ”] 5.10. Measure and guard the memory usage

[โœ”๏ธ] 5.11. Get your frontend assets out of Node

![โ”] 5.12. Be stateless, kill your servers almost every day

[โœ”๏ธ] 5.13. Use tools that automatically detect vulnerabilities

![โ”] 5.14. Assign a transaction id to each log statement

[โœ”๏ธ] 5.15. Set NODE_ENV=production

![โ”] 5.16. Design automated, atomic and zero-downtime deployments

![โ”] 5.17. Use an LTS release of Node.js

![โ”] 5.18. Don't route logs within the app

6. Security Best Practices

[โœ”๏ธ] 6.1. Embrace linter security rules

[โœ”๏ธ] 6.2. Limit concurrent requests using a middleware

[โœ”๏ธ] 6.3 Extract secrets from config files or use packages to encrypt them

[โœ”๏ธ] 6.4. Prevent query injection vulnerabilities with ORM/ODM libraries

![โ”] 6.5. Collection of generic security best practices

[โœ”๏ธ] 6.6. Adjust the HTTP response headers for enhanced security

[โœ”๏ธ] 6.7. Constantly and automatically inspect for vulnerable dependencies

[โœ”๏ธ] 6.8. Avoid using the Node.js crypto library for handling passwords, use Bcrypt

![โ”] 6.9. Escape HTML, JS and CSS output

[โœ”๏ธ] 6.10. Validate incoming JSON schemas

![โ”] 6.11. Support blacklisting JWTs

![โ”] 6.12. Prevent brute-force attacks against authorization

[โœ”๏ธ] 6.13. Run Node.js as non-root user

[โœ”๏ธ] 6.14. Limit payload size using a reverse-proxy or a middleware

![โ”] 6.15. Avoid JavaScript eval statements

![โ”] 6.16. Prevent evil RegEx from overloading your single thread execution

[โœ”๏ธ] 6.17. Avoid module loading using a variable

![โ”] 6.18. Run unsafe code in a sandbox

![โ”] 6.19. Take extra care when working with child processes

[โœ”๏ธ] 6.20. Hide error details from clients

[โœ”๏ธ] 6.21. Configure 2FA for npm or Yarn

[โŒ] No neccessary - 6.22. Modify session middleware settings

![โ”] 6.23. Avoid DOS attacks by explicitly setting when a process should crash

[โŒ] No neccessary - 6.24. Prevent unsafe redirects

[โœ”๏ธ] 6.25. Avoid publishing secrets to the npm registry

7. Performance Best Practices

Our contributors are working on this section. Would you like to join?

[โœ”๏ธ] 7.1. Prefer native JS methods over user-land utils like Lodash

[โ”] 7.2. Use Fastify in place of Express

More Repositories

1

nestjs-restful-best-practice

NestJS (Express + TypeORM + MongoDB) codebase containing real world examples (CRUD, auth, advanced patterns, etc).
TypeScript
151
star
2

react-sendbird-messenger

ReactJS (React-router-dom v6 + Antdesign + Firebase + Sendbird + Sentry) codebase containing real world examples (CRUD, auth, advanced patterns, etc).
JavaScript
51
star
3

reactnative-best-practice

React Native (ReactJS + ReactNavigation5 + Firebase) codebase containing real world examples (CRUD, auth, advanced patterns, etc).
JavaScript
49
star
4

chnirt-demo-nest-typeorm-mongodb

TypeScript
48
star
5

react-native-janus

JavaScript
8
star
6

nestjsv7-graphql-best-practice

TypeScript
4
star
7

swiftui-best-practice

NestJS (SwiftUI + GraphQL) codebase containing real world examples (CRUD, auth, advanced patterns, etc).
Swift
3
star
8

chnirt-chewapp

React (Webpack + Material + Hooks + Context) codebase containing real world examples (auth, advanced patterns, etc).
JavaScript
3
star
9

nextjs-tailwind-landing-page

JavaScript
2
star
10

nestjs-gateway

TypeScript
2
star
11

do-what

built ReactNative, Recoil
TypeScript
2
star
12

mongooseapi

JavaScript
1
star
13

order-drink

JavaScript
1
star
14

rn-signal

JavaScript
1
star
15

nestjs-federation

TypeScript
1
star
16

golang-demo

Go
1
star
17

rn-autodeploy

Java
1
star
18

custom-tui-calendar

Created with CodeSandbox
JavaScript
1
star
19

react-apollo-graphql

JavaScript
1
star
20

chnirt-demo-nest-mongoose-mongodb

TypeScript
1
star
21

gatsby-graphql-best-practice

built Gatsby, GraphQL
CSS
1
star
22

apollo-react-hooks

JavaScript
1
star
23

rn-teacherapp

TypeScript
1
star
24

happy-birthday-card

Created with CodeSandbox
1
star
25

react-family-tree

JavaScript
1
star
26

chnirt-movie

TypeScript
1
star
27

nestjs-lambda-zeit

TypeScript
1
star
28

reactnative-greenweed

Objective-C
1
star
29

lunchapp-frontend

built in ReactJs, Mobx, GraphQL, Ant-design, i18n, Webpack, Cypress
JavaScript
1
star
30

chnirt-rocket

JavaScript
1
star
31

vite-firebase-chat-app

Integrate new technology
JavaScript
1
star
32

install-act

Run your GitHub Actions locally!
1
star