-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.coffee
42 lines (31 loc) · 1022 Bytes
/
server.coffee
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
express = require 'express'
bodyParser = require 'body-parser'
fs = require('fs');
domain = require 'domain';
ServiceError = require './src/exceptions/ServiceError'
app = module.exports = express();
app.use bodyParser.urlencoded {extended: true}
app.use bodyParser.json()
controllerLoader = (router) ->
load = (file) ->
if file.indexOf('.coffee') isnt -1
console.log file
controller = require './src/controllers/' + file
controller(router)
load f for f in fs.readdirSync './src/controllers'
app.use "/", (err,req,res,next) ->
requestDomain = domain.create();
requestDomain.add req;
requestDomain.add res;
requestDomain.on 'error', next;
requestDomain.run next;
router = express.Router();
controllerLoader router
app.use '/', router
app.use (err,req,res,next) ->
if err instanceof ServiceError
res.status(err.statusCode)
res.send err.message;
else if err instanceof Error
res.status(500)
res.send err.message;