Code Monkey home page Code Monkey logo

waqarilyas / node-typescript-boilerplate Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 1.0 227 KB

Node.js TypeScript boilerplate project with MongoDB and JWT authentication. The project is built using the Express framework and follows an MVC architecture pattern. It includes separate folders for routes, models, services, and middleware for easy management of code.

License: MIT License

JavaScript 0.09% Shell 0.26% TypeScript 99.65%
boilerplate boilerplate-template mvc mvc-architecture nodejs nodejs-express-mongodb nodejs-express-typescript nodejs-typescript-template passportjs typescript

node-typescript-boilerplate's Introduction

Node.js TypeScript Template with Express and MongoDB

This is a TypeScript template for a Node.js project using the Express framework and MongoDB as the database. The template contains the following features:

  • Routes management with versioning and role-based route access.
  • Passport JWT role-based authentication.
  • MVC architecture with separate folders and code for routes, models, services, and middlewares.
  • Session management with JWT token.
  • API rate limit.
  • Generic response management i.e. error and success responses.
  • Env management with validation.
  • Full TypeScript support.
  • Code validation with tests. Tests are written using chai.

The project includes code for user login, signup, logout, creating, updating, and deleting tweets with user feed management.

Project Structure

  • src
    • config
      • config.ts
      • Db.ts
      • Logger.ts
      • Morgan.ts
      • Passport.ts
      • roles.ts
      • Tokens.ts
    • controllers
      • auth.controller.ts
      • tweet.controller.ts
    • middlewares
      • Auth.ts
      • Error.ts
      • rateLimiter.ts
      • Validate.ts
    • models
      • like.ts
      • tweet.ts
      • token.ts
      • wallet.ts
      • user.ts
    • routes
      • auth.ts
      • tweet.ts
    • services
      • like.service.ts
      • tweet.service.ts
      • token.service.ts
      • wallet.service.ts
      • user.service.ts
    • tests
      • auth.test.ts
      • tweet.test.ts
    • validations
      • auth.validation.ts
      • tweet.validation.ts

Setup

  • Clone this repository.
  • Run npm install to install all dependencies.
  • Create a .env file in the root directory and add your environment variables (refer to config/config.ts for required variables).
  • Run npm run start to start the server.
  • Access the server at http://localhost:3000.

Usage

Routes

All routes are defined in the routes folder. There are currently two routes: auth and tweet.

Auth Routes

  • POST /auth/signup: creates a new user.
  • POST /auth/login: logs in an existing user.
  • POST /auth/logout: logs out the currently logged in user.

Tweet Routes

  • GET /tweets: gets all tweets.
  • GET /tweets/:id: gets a single tweet by ID.
  • POST /tweets: creates a new tweet.
  • PUT /tweets/:id: updates an existing tweet by ID.
  • DELETE /tweets/:id: deletes an existing tweet by ID.

Authentication

All routes except for POST /auth/signup and POST /auth/login require authentication. Authentication is done using Passport JWT and the Auth middleware in the middlewares folder.

Validation

All request bodies, query parameters, and URL parameters are validated using the Validate middleware in the middlewares folder.

Rate Limiting

API rate limiting is implemented using the rateLimiter middleware in the middlewares folder.

Response Management

All responses are managed using the Error and Success middlewares in the middlewares folder.

Tests

Tests are written using Chai and can be found in tests folder.

node-typescript-boilerplate's People

Contributors

waqar5 avatar waqarilyas avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

emmer12

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.