Skip to content

A Starter Backend Setup With Express and Sequelize with Typescript

Notifications You must be signed in to change notification settings

Tudiman555/Express-Sequelize-Setup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Basic Express Server Setup with Typescript and Sequelize

Prerequisites

  • Node.js Should be installed
  • Mysql Should be installed

Libraries Used

  • mysql2 --> Standard Nodejs mqsql driver
  • sequelize --> Promise Based Node.js ORM for SQL
  • express --> Light Weight Web FrameWork for Node.js
  • express-validator --> Express Middleware for validation
  • body-parser --> Middleware for parsing body of a request
  • cookie-parser --> Middleware for parsing cookies of a request
  • cors --> Facilitates CORS (Cross Origin Resourse Sharing )
  • bcrypt --> A library used for hashing passwords
  • jsonwebtoken --> Used for JSON web tokens
  • typescript --> Used for Providing Types to help debug and catch errors before they go into production
  • nodemon --> Used as Dev Dependency to restart server whenever there are file changes
  • eslint --> Helps to find and debug certain problematics patterns and bugs in javascript
  • prettier --> Code Formatting Tools which helps you maintain consistent code pattern
  • eslint-config-prettier --> Turns off all rules that are unnecessary or might conflict with [Prettier]

Setup (If you want to setup your project from Scratch)

Install the following dependencies :

    1. Express.js with Sequelize ORM and Mysql driver

npm i express sequelize mysql2

    1. express.js is lightweight and plugin-oriented, which means that we need to install some plugins/middleware to handle everything beyond basic functionality

npm i -S body-parser cookie-parser cors express-validator

    1. Include user’s authentication into app, so we need libraries for handling jwt tokens and password hashing

npm i -S jsonwebtoken bcrypt >

    1. We need TypeScript with types

npm i -D typescript @types/bcrypt @types/body-parser @types/cookie-parser @types/cors @types/es6-promise @types/express @types/express-validator @types/jsonwebtoken @types/node @types/sequelize

    1. Install Nodemon , Eslint and Prettier

npm install --save-dev nodemon eslint prettier eslint-config-prettier eslint-plugin-prettier @typescript-eslint/parser @typescript-eslint/eslint-plugin

Project Configuration :

    1. Create a Entry File Named server.ts

touch server.ts

This will be your app entry file

    1. Create a Express Server with a Port Inside server.ts
import * as express from "express";

const app = express();
const port = 4001;

app.get("/", (req, res, next) => {
  res.json("Hello Guys");
});

app.listen(port, () => {
  console.log(`App is listening on port ${port}`);
});
    1. Configure Typescript

Create a file named tsconfig.json in the root dir of the project and add the following configuration

{
  "compilerOptions": {
    "outDir": "dist",
    "sourceMap": true,
    "module": "commonjs",
    "target": "es2017"
  },
  "include": ["src/**/*"]
}

include --> specifies which directory you want typescipt to transpile/watch outDir --> specifies in which directory should transpiled file go to

    1. Configure Scripts inside package.json

Inside package.json Specify the name of the entry file of the server under main field which will the server.js it will be transpiled by typescript Inside package.json Add the Following scripts under scripts field

 "watch-ts": "tsc -w",
 "watch-node": "nodemon dist/server.js",
 "watch-debug": "npm run watch-ts & npm run watch-node"

watch-ts --> instructs typescript to watch files in directory specified in tsconfig.json for changes and transpile the ts files into js and store the result in dist directory as specified in tsconfig.json

watch-node --> run the transpiled files and automatically re-run the files everytime file changes.

watch-debug --> performs transpiling and execution concurrently [This Command will be used to run the app]

    1. Running the App

Inside the root Directory run the following command

npm run watch-debug

you can see the server running on localhost:4001 inside browser

And Done !!!

    1. Adding Squelize with typescript

npm install --save-dev @types/node @types/validator npm install reflect-metadata sequelize-typescript

Enable use of Decorators in your tsconfig.json :

"target": "es6", // or a more recent ecmascript version
"experimentalDecorators": true,
"emitDecoratorMetadata": true

Add the above properties to compilerOptions

    1. Initialize the Database and test your connection

Refer ./src/services/database.service.ts

About

A Starter Backend Setup With Express and Sequelize with Typescript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published