Coder Social home page Coder Social logo

norkator / node-express-orm-typescript-template Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 2.0 352 KB

Simple Node.js, TypeScript, Express, JWT, Sequelize Orm API, Schedule application template for fast need with proper documentation

License: MIT License

TypeScript 97.83% JavaScript 1.28% Dockerfile 0.89%
typescript orm-template nodejs-api typescript-api api-template express-template express-typescript-template sequelize-typescript sequelize-express typescript-application typescript-app

node-express-orm-typescript-template's Introduction

'Swagger'

Node-Express-Orm-TypeScript-Template

Simple Node.js, Express, TypeScript, Sequelize Orm app api template with proper documentation to get started.

Goal of this repository is to build simple well documented TypeScript based Node.JS Express application api which is easy and fast base for agile api development when time is limited. It's work in progress.

It's not only meant to be api but also application running tasks with node-scheduler. I am personally using tasks to update specific database information while serving api for front end applications.

Contributions are very very welcome and highly appreciated!


Table of contents


Sources

I originally used api generator from this source. I found this initially complicated to get started with if no proper experience with TypeScript API's. I later added different structure, https and scheduled tasks support.

Version notes

At the time of writing this TypeScript used was on version: 3.9.5

Installation

Package setup

Run following commands one at the time, once.
Lines have comments explaining basics what these packages do.
npm -g means global installation basically working from CLI, it's same as giving --global flag.

npm install -g nodemon          # Needed to listen for changes when developing
npm install -g ts-node          # Allows you to run TypeScript in Node. js directly, without having to run the files through the TypeScript compiler (tsc), first
npm install -g typescript       # TypeScript is a language for application-scale JavaScript, it's language you are using for development here.
npm install                     # Installs all dev/dependencies from packages.json file.

Environment settings

Next create .env file using .env_template contents as example.
Place this file in same folder as template file already is.

Start dev server

Run following command

nodemon

Result should look like this:

'nodemon'

Open web browser and navigate to http://localhost:3000/ depending of port you set at .env file.

Documentation

Beginning

See nodemon.json file at the root of this project. Inside there you find reference to file /src/index.ts

Server index.ts is where all begins. It works with following components:

index.ts

dbTools                # Checks database existence, creates it if not based on .env details
createServer           # Either http or https controlled by .env file, production use https with valid cert like letsencrypt
eventHandlers          # Functions which are triggered on specific events like on error event.
app                    # Leads to /src/app/ structure. Open index.ts there to specify tasks.

server.ts

express             # creates express app
middleware          # load middleware, this is explained more later
routes              # This is where your routes like /book/get are described and then logic is behind this
process variables   # At server.ts app get's set it's process variables, loaded from .env file or set as default if not specified

middleware.ts
Middleware has following functions:

bodyparser          # Parse incoming request bodies in a middleware before your handlers, available under the req.body property.
cookieParser        # Parse Cookie header and populate req.cookies with an object keyed by the cookie names.
compression         # Attempt to compress response bodies for all request that traverse through the middleware, based on the given options.
helmet              # Helps you secure your Express apps by setting various HTTP headers.
cors                # Connect/Express middleware that can be used to enable CORS with various options.
sendHttpErrorModule # Custom request filter to send error code on non valid request. 
logRequestPaths     # Live logging requested paths, can be disabled since console logging is synchronous, meaning that it affects performance.

Adding new route

Adding new router or route starts from /routes folder via copy paste existing router like ExampleRouter.ts to a new name like NewRouter.ts

'NewRouter1'

Then you need to create new component which has required method logic

'NewRouter2'

Inside index.ts has again copy pasted content from Example component which is returning json result ok for post request

'NewRouter3'

Now go to following index.ts file under /routes folder

'NewRouter4'

At the routes index file you create base route for your new NewRouter route like this:

'NewRouter5'

Also you need to import this NewRouter

'NewRouter6'

Since this is post method which was defined at NewRouter.ts, result will be for post request:

'NewRouter7'

Route authentication

See ExampleRouter.ts for good example how you can define part of route under authentication.
'JwtAuth1'

This means that you can access http:localhost:port/example without authentication and then define under this path which components require authentication.

Or you define whole in this case http:localhost:port/new route requiring always authentication no matter which method is called behind it.
'JwtAuth2'

Calling routes

There is a file called ApiTesting.http included in root of this project. That file can be run at least on Intellij IDE's having integrated REST client like Postman.

'Swagger'

'Swagger'

Authors

Contributors

None so far.

node-express-orm-typescript-template's People

Contributors

dependabot[bot] avatar norkator avatar

Stargazers

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