-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
38 lines (34 loc) · 1.34 KB
/
Copy pathindex.js
File metadata and controls
38 lines (34 loc) · 1.34 KB
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
var koa = require('koa');
var app = koa();
var router = require('koa-router');
var mount = require('koa-mount');
var api = require('./api/api.js');
var logger = require('koa-logger');
var limit = require('koa-better-ratelimit');
var compress = require('koa-compress');
var opts = {
filter: function (content_type) { return /text/i.test(content_type) }, // filter requests to be compressed using regex
threshold: 2048, //minimum size to compress
flush: require('zlib').Z_SYNC_FLUSH };
}
var APIv1 = new router();
APIv1.get('/all', api.all);
APIv1.get('/single', api.single);
app.use(compress(opts));
app.use(function *(next){
try{
yield next; //pass on the execution to downstream middlewares
} catch (err) { //executed only when an error occurs & no other middleware responds to the request
this.type = 'json'; //optional here
this.status = err.status || 500;
this.body = { 'error' : 'The application just went bonkers, hopefully NSA has all the logs ;) '};
//delegate the error back to application
this.app.emit('error', err, this);
app.use(limit({ duration: 1000*60*3 , // 3 min
max: 10, blacklist: []}));
app.use(logger());
}
});
app.use(mount('/v1', APIv1.middleware()));
if (!module.parent) app.listen(3000);
console.log('Dictapi is Running on http://localhost:3000/');