-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.coffee
51 lines (38 loc) · 1009 Bytes
/
service.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
43
44
45
46
47
48
49
50
51
express = require 'express'
{ Router } = require 'express'
Generator = require './lib/girls'
generator = new Generator()
process.on 'uncaughtException', (err) ->
console.error err
app = express()
router = new Router()
postHandler = (req, res, next) ->
data = req.body
generator.generate data, (err, result) ->
if err?
next err
else
res.status(200).json result
router.post '/', postHandler
apiBodyParser = require('body-parser').json()
app.use '/', apiBodyParser, router
errorHandler = (err, req, res, next) ->
switch err.name
when 'BadRequestError'
status = err.status or 400
response =
message: err.message or 'BadRequest'
name: err.name or 'BAD_REQUEST'
else
status = err.status or 500
response =
message: err.message or 'Internal Server Error'
name: err.name or 'SERVER_ERROR'
error =
status: status
name: err.name
message: response.message
res.error = error
res.status(status).json(response)
app.use errorHandler
module.exports = app