DEV Community

Bhoomit Ganatra
Bhoomit Ganatra

Posted on

8

Nodejs and typescript boilerplate

Node Boilerplate Typescript

  • This repo is boilerplate for typescript and nodejs projects

What is included

  • Server with express
  • Security Packages
    • hpp - Express middleware to protect against HTTP Parameter Pollution attacks
    • helmet - Helmet helps secure Express apps by setting HTTP response headers.
  • File uploading
  • Sending Emails
  • Logging
    • By default it is configured to log on console but you can extend it to write to any file as well
  • Object(s) Validation using zod
  • Error Handling
  • Express Req object extend with typescript support

Routing

  • The base path for api will be api.
  • Any folder you put inside modules folder that container routes.ts file will be added to express router stack and that will be route for the api as well.
  • So suppose if you make folder something like modules/v1/file/routes.ts then all the routes defined in that routes.ts file will be registered and will be available at domain/api/modules/v1/file.
  • So think like routing as a file based routing.
  • You must export router as default export from routes.ts file to make this happen.

Error handling

  • You can alway use try catch in your controllers to handle errors by your self, but here I have used different approach.
  • Just wrap your any controller/middleware that receives express's req,res object wrap them in PromiseHandler function and this will handle the rest of the stuff.
  • Also you can create custom errors in error folder as per your requirement and handle it in handleApiError according the way you want.

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay