-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex_restify.js
39 lines (28 loc) · 937 Bytes
/
index_restify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const restify = require('restify');
const redis = require('redis');
require('dotenv').config();
const RateLimit = require('./middleware/rate_limit');
const PORT = process.env.PORT;
const redisHost = process.env.REDIS_HOST;
const redisPort = process.env.REDIS_PORT;
const redisPassword = process.env.REDIS_PASSWORD;
const redisDB = process.env.REDIS_DB;
const redisClient = redis.createClient({
host: redisHost,
port: redisPort,
password: redisPassword,
db: redisDB
});
// create RateLimit instance
const rateLimit = new RateLimit({redisClient: redisClient, points: 5, duration: 1});
// create restify
const server = restify.createServer();
// set rate limit middleware
server.use(rateLimit.setRateLimiterMiddleware());
server.get('/', (req, res, next) => {
res.send({'message': 'hello'});
next();
});
server.listen(PORT, function() {
console.log('%s listening at %s', server.name, server.url);
});