DEV Community

Mitesh Kamat
Mitesh Kamat

Posted on

4 1

Securing express js server

Introduction

This post is about securing your express js application from network attacks.

Once you are done with developing your application which uses node js server, the next task comes up is of deploying it to production or making it ready for production.

First and the most vital point that comes to our mind is how secure is our application? Are the API requests made through my application secure? Does the secret key or token or sensitive data has enough security?

Express JS documentation covers all these questions here

It lists down the options we should opt for and other suggestions.

In my case, I made use of Helmet , an npm package which provides enough security to our routes.
You can also customize the Content-Security-Policy header as per your needs.

app.use(helmet.contentSecurityPolicy({
  directives: {
    defaultSrc: ["'self'"],
    styleSrc: ["'self'", 'maxcdn.bootstrapcdn.com'],
    scriptSrc: ["'none'"],
    imgSrc: ["'none'"],
    fontSrc: ["'none'"],
  }
}))
Enter fullscreen mode Exit fullscreen mode

As mentioned in the options, you can specify the valid sources for your images, fonts, scripts, styles, etc. This surely adds a level of security.
Refer this for contentSecurityPolicy.

I am sure it will help you in some way.

Cheers !!!

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

nextjs tutorial video

📺 Youtube Tutorial Series

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

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

Okay