-
Notifications
You must be signed in to change notification settings - Fork 26
/
server.js
46 lines (37 loc) · 1.06 KB
/
server.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
40
41
42
43
44
45
46
'use strict';
var Restify = require('restify');
var debug = require('debug')('flint-serve');
var _ = require('lodash');
// create / configure restify server
var restify = Restify.createServer({
'name': 'flint'
});
restify.use(Restify.bodyParser({
'mapParams': true,
'mapFiles': false,
'overrideParams': true
}));
restify.use(Restify.queryParser());
module.exports = {
listen: function(host, port) {
restify.listen(port, host, function() {
debug('is now listening on http://%s:%s',host, port);
});
},
route: function(method, resource, fn, cb) {
method = method.toLowerCase();
resource = resource.toLowerCase();
var methods = ['get', 'post', 'put', 'del'];
// check if method is valid
if(!_.includes(methods, method)) {
return cb ? cb(new Error('invalid method %s', method)) : null;
}
// add route to restify
restify[method](resource, function(req, res, next) {
// execute function
fn(req, res, next);
});
debug('added route for %s:%s', method, resource);
return cb ? cb(null) : null;
}
};