kdocs
GitHub
Lang - Runtime
Lang - Runtime
  • Go
    • Modules & Packages
    • Variables
    • Structs
    • Interfaces
    • Functions
    • Control Structures
    • Erro Handling
    • Concurrency
    • Testing
    • Fuzzing
  • Web
    • V8 Engine
    • Node.js
      • New Project
    • Deno
      • New Project
Powered by GitBook
On this page
  • Typescript Environment
  • Commands & Configs
  • Running
  • Express Environment
  • Commands & Configs
  1. Web
  2. Node.js

New Project

Tips for initializing new blank projects

Typescript Environment

Basic environment for executing Typescript scripts.

Commands & Configs

Initialize Node Project

npm init --init-author-name "your-email" -y

Installing TS on Node

Node by default don't recognize Typescript only Javascript.

npm install --save-dev @types/node
npm install --save-dev typescript

Generate the tsconfig.json file:

npx tsc --init \
--outDir build \
--module commonjs \
--allowJs true \
--removeComments true

or for a more strict configuration:

npx tsc --init \
--outDir build \
--esModuleInterop \
--resolveJsonModule \
--module commonjs \
--allowJs true \
--noImplicitAny true \
--removeComments true

You can add these to configure the folders to include and exclude in transpilation:

tsconfig.json
{
    "compilerOptions": {

    },
    "include": ["./src"],
    "exclude": ["./tests"]
}

Configure Custom Running Commands

To be executed with npm.

# For Windows only (To execute the removal files/folders)
npm install --save-dev rimraf
package.json
{
    "scripts": {
        // If Windows
        "build": "rimraf ./build && tsc",
        // If Linux
        "build": "rm -rf ./build && tsc",
        "test-build": "node build/main.js",
    }
}

The building output directory may be configured in the tsconfig.json file.

Optional: Install nodemon (For Cold Reloading)

To auto reload the project when files change.

npm install --save-dev ts-node nodemon
package.json
{
    "scripts": {      
        "dev": "nodemon --watch src -e ts --exec ts-node src/main.ts"
    }
}
nodemon.json
{
    "watch": ["src"],
    "ext": ".ts,.js",
    "ignore": [],
    "exec": "npx ts-node ./src/main.ts"
}

Optional: Install dotenv (For Env variables)

npm install --save-dev dotenv
nodemon.json
{
    "exec": "npx ts-node  ./src/main.ts"    
}

Optional: Install tsconfig-path (???)

npm install --save-dev tsconfig-paths

.gitignore

.DS_Store
node_modules
build
coverage
.env
!.env.example

Running

To run + openning the browser:

npm run dev -- --open

Just running the server:

npm run dev

Express Environment

Basic environment for executing an Express (RestAPI) project.

Commands & Configs

Initialize the Project

Follow Commands & Configs from Typescript Env.

Install Main Dependencies

npm install --save express
npm install --save express-validator
npm install --save helmet
npm install --save body-parser
npm install --save-dev @types/express

Optional: Mkdirp

To run mkdir -p for create folders if don't exist.

npm install --save mkdirp

Optional: JWT Tokens (For JWT Token generation)

npm install --save jsonwebtoken
npm install --save-dev @types/jsonwebtoken

Optional: Async (Async/Parallel execution)

npm install --save async

Optional: Mysql (Mysql connector)

npm install --save mysql2

Optional: Axios

npm install --save axios

Optional: Express-Validator

For Body, Query and Param data validation/sanitization.

npm install --save express-validator
PreviousNode.jsNextDeno

Last updated 4 months ago

GitHub - auth0/node-jsonwebtoken: JsonWebToken implementation for node.js http://self-issued.info/docs/draft-ietf-oauth-json-web-token.htmlGitHub
Axios API | Axios Docs
Logo
express-validator | express-validator
Logo
Logo