Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use helmet.js from typescript?

It is pretty easy to use helmet from pure js per documentation:

const express = require('express')
const helmet = require('helmet')

const app = express()

app.use(helmet())

But how can I use it from typescript? Typings file exports a bunch of stuff, one of which is helmet interface, which cannot be called as a function.. I can import it like this, but have no idea what to do next, what should I pass to app.use?

import * as helmet from 'helmet'

I have imported the latest version of both helmet and typings:

"@types/helmet": "0.0.43",
"helmet": "^3.18.0"
like image 973
eugenekr Avatar asked Dec 01 '22 13:12

eugenekr


1 Answers

terminal:

npm install helmet
npm install @types/helmet --save-dev

app.ts:

import Helmet from "helmet";

const app = express();
app.use(Helmet());

helmet middleware should be the first thing you activate once you initialized express application object. Also note the titlecase name

like image 129
Eyal Israel Avatar answered Dec 14 '22 23:12

Eyal Israel