Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

express-rate-limit with static

I have VPS with express and react bundle. The problem is that I get the same IP address (localhost) when I access the API from the frontend, therefore I cannot correctly use the express-rate-limit.

I have an express server:

const apiLimiter = rateLimit({
  windowMs: 1 * 60 * 1000,
  max: 30
});

app.use("/api/", apiLimiter);

app.use(express.static('client/build'));
app.get('*', (req, res) => {
  res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});

and proxy config in package.json of frontend:

"proxy": "http://localhost:3000/"

How to fix it and use express-rate-limit correctly?

like image 852
Jon Avatar asked Nov 06 '22 03:11

Jon


1 Answers

Per https://www.npmjs.com/package/express-rate-limit#usage

app.set('trust proxy', 1)

like image 147
Michael Hobbs Avatar answered Nov 12 '22 13:11

Michael Hobbs