Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import logger from './logs/log'
import userAgent from 'koa-useragent'
import error from 'koa-json-error'
import ratelimit from 'koa-ratelimit'
import redis from 'ioredis'
//Routes
import userActionsRouter from './routes/userActions'
import notesRouter from './routes/notes'
//Initialize app
const app = new Koa()
//Here's the rate limiter
app.use(
ratelimit({
db: new redis(),
duration: 60000,
errorMessage:
"Hmm, you seem to be doing that a bit too much - wouldn't you say?",
id: ctx => ctx.ip,
headers: {
remaining: 'Rate-Limit-Remaining',
reset: 'Rate-Limit-Reset',
total: 'Rate-Limit-Total',
},
max: 100,
})
)
//Let's log each successful interaction. We'll also log each error - but not here,
//that's be done in the json error-handling middleware
import Koa from "koa";
import ratelimit from "koa-ratelimit";
import compress from "koa-compress";
import logger from "koa-logger";
import mount from "koa-mount";
import cors from "./middlewares/cors";
import redis from "./db/redis";
import api from "./api";
const app = new Koa();
if (process.env.NODE_ENV === "production") {
app.use(
ratelimit({
db: redis,
duration: 60000,
errorMessage:
"Please stop hitting us so hard. Please deploy your own instance of the API",
id: ctx => ctx.get("X-Real-IP"),
headers: {
remaining: "Rate-Limit-Remaining",
reset: "Rate-Limit-Reset",
total: "Rate-Limit-Total"
},
max: 50
})
);
}
app.use(compress());
app.use(cors());
throttle(options){
if (options) this.koa.use(convert(throttle(options)));
return this;
}