Giter Club home page Giter Club logo

better-express-api-structure-ts's Introduction

Better Express API Structure

Introduction

  • In today's node development, most of the time we tend to create an web api server and all the things around it (mostly).
  • We have a certain steps of work needs to be done before actually work on your project, like creating the web server, attaching the api endpoints to it, error handling middlewares and all.
  • Also, it have to be in proper structure with all the things segregated from one another.
  • To do all this things we utilize the first couple of hours in it.
  • Bonus steps will be added to create the development with typescript.
  • So, to eradicate this problem, I have created this repository.

Overview

  • This is a web server with typescript.
  • Below are the packages used in this project.
    • express : Fast, unopinionated, minimalist web framework for Node.js
    • cookie-parser : To get the cookies from the request in the request.cookies.
    • cors : To enable CORS.
    • morgan : HTTP request logger middleware.
    • dotenv : To load the environment variables from .env files into process.env.
    • swagger-jsdoc : Reads the JSDoc-annotated source code and generates an OpenAPI(Swagger) specification.
    • swagger-ui-express : To serve auto-generated swagger-ui generated API docs from express.
    • helmet : To secure the app by setting various HTTP headers.
    • express-bouncer : To mitigate brute-force attacks.
  • To run the typescript in development, I used the nodemon + ts-node combo.

Usage

  • The application provides some scripts for building and running the application in development and production.
    "dev": "nodemon src/app.ts",
    "pre-build": "rm -rf ./dist/",
    "build": "npm run pre-build && tsc -p . && npm run post-build",
    "post-build": "cp -r ./src/public ./dist/public",
    "start": "node dist/app.js"
  • Update the .env as per your configuration.

  • Also, when you use nodemon for debugging/hot-reloading the application, please change the APP_ENVIRONMENT to development.

# .env
APP_ENVIRONMENT=development
  • /swagger to get the swagger API documentation page, where all the dummy api will be shown.

Check the .env for the APP_ENVIRONMENT value, change the value as in development or production

Open Issues

  • Don't know which will be good practice to export and imports.
  • Configure Cookie Parser for the following in typescript
app.use(cookieParser(COOKIE_SECRET, {
  secure: true,
  httpOnly: true,
  domain: DOMAIN,
  expires:
}));

Next Steps

  • Improvise the project by establishing some security aspects of node.

For any suggestions, or bugs, please raise issues.

better-express-api-structure-ts's People

Contributors

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