Git Product home page Git Product logo

template-nodejs-api's Introduction

Template NodeJS APIs

  • This repo for define template for reuse any Project APIs in NodeJS or GraphQL
  • This Template using NestJS Framework for development and can make issues at HERE
  • Discord Channel of NestJS Framework for more discussion.

Configuration

  1. Create a .env file from .env.sample and update value before start.
  2. Edit env config
    • Edit the file in the src/config folder.
    • Update value for each ENVIRONMENT at src/config/envs/{ENV_NAME}.ts, example: default / development / production / test
  3. Activating and configuring ESLint

Setup on WebStorm

  • In the Settings/Preferences dialog (Ctrl+Alt+S), go to Languages and Frameworks | JavaScript | Code Quality Tools | ESLint.
  • Select the Manual ESLint configuration option to use a custom ESLint package and configuration.
  • In the Node Interpreter field, specify the path to Node.js. If you followed the standard installation procedure, WebStorm detects the path and fills in the field itself.
  • In the ESLint Package field, specify the location of the eslint or standard package. {project_path}/node_modules/eslint
  • In the Configuration file field, select this option to use a custom file and specify the file location in the Path field. {project_path}/.eslintrc
  • Click Apply > OK

Setup on VS Code

Installation

  • This use for setup
  • If you use multiple databases then need change some in bin/entity.js at Line 52
# 1. node_modules - just for NPM
npm ci && npm i
# or
yarn install 
# 2. When import entities from an existing database
npm run entity
# or
yarn run entity

Environments

Development

  • Start browser at http://localhost:{port_in_env_file} after run command under.
npm run dev

Test

  • For run test with unittest, e2e in codebase
npm test # exclude e2e
npm run e2e

Production

  • Use only for production env
npm run build
# define environment variable yourself.
# NODE_ENV=production PORT=8000 NO_COLOR=true node dist/app
node dist/app
# or
npm start
# or
yarn start

Folders

+-- bin // Custom tasks
+-- dist // Source build
+-- public // Static Files
+-- src
|   +-- config // Environment Configuration
|   +-- entities // TypeORM Entities generated by `typeorm-model-generator` module
|   +-- modules // Store all module source here each sub-folder is one module and it's includes all files in this
|   |   +-- auth // Authentication module
|   +-- common // Global Nest Module
|   |   +-- base // base class for project
|   |   +-- constants // Constant value and Enum
|   |   +-- controllers // Nest Controllers
|   |   +-- decorators // Nest Decorators
|   |   +-- dto // DTO (Data Transfer Object) Schema, Validation
|   |   +-- filters // Nest Filters
|   |   +-- guards // Nest Guards
|   |   +-- loggers // All logger folder
|   |   +-- interceptors // Nest Interceptors
|   |   +-- interfaces // TypeScript Interfaces
|   |   +-- middleware // Nest Middleware
|   |   +-- pipes // Nest Pipes
|   |   +-- providers // Nest Providers
|   |   +-- models // Global models
|   |   +-- services // Global services
|   |   +-- * // repositories...
|   +-- shared // Shared Nest Modules
|   +-- debug // custom debug
|   +-- gql // GraphQL Structure
|   +-- * // Other Nest Modules, non-global, same as common structure above
+-- tests // Jest testing
+-- typings // Modules and global type definitions

// Module structure
// Add folders according to module scale. If it's small, you don't need to add folders.
+-- src/modules/greeter
|   +-- * // folders
|   +-- greeter.constant.ts // all constant for this module
|   +-- greeter.controller.ts // controller file for this module
|   +-- greeter.service.ts // service file for handle business logic and communicate with model for response data to controller
|   +-- greeter.module.ts // sub-module file to setup module for NestJS
|   +-- greeter.*.ts
|   +-- index.ts

Implements

Documentation

# APP, Compodoc
npm run doc #> http://localhost:8080
# API, Swagger - src/swagger.ts
npm run api #> http://localhost:8000/api

Rules And References

  • Some rules for write code in this template

TypeScript Docs Refers

Variables Naming

Linter

It is recommended to apply the known extended presets in addition to the basic rules

Conventional Commits: For consistent commit message

Semantic Versioning: For automatic versioning and changelog based on consistent commit messages

Index Exporting

# It's recommended to place index.ts in each folder and export.
# Unless it's a special case, it's import from a folder instead of directly from a file.
- import { FooController } from './controllers/foo.controller';
- import { BarController } from './controllers/bar.controller';
+ import { FooController, BarController } from './controllers';
# My preferred method is to place only one fileOrFolder name at the end of the path.
- import { UtilService } from '../common/providers/util.service';
+ import { UtilService } from '../common';

Circular Dependency

# Do not use a path that ends with a dot.
- import { FooService } from '.';
- import { BarService } from '..';
+ import { FooService } from './foo.service';
+ import { BarService } from '../providers';

Links

template-nodejs-api's People

Contributors

longthemartec 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.