Created
April 20, 2017 16:29
-
-
Save yoshuawuyts/3920b9f4e67052dcd819340d8eee6b2f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var merry = require('merry') | |
var env = merry.env({ | |
NODE_ENV: String, | |
PORT: Number | |
}) | |
var errors = merry.errors([ | |
{ | |
name: 'ENOTFOUND', | |
statusCode: 400, | |
type: 'invalid_request_error', | |
message: 'The route you requested does not exist.', | |
docs: 'https://docs.myapp.io/routes' | |
}, | |
{ | |
name: 'EDATABASE', | |
statusCode: 500, | |
type: 'api_error', | |
message: 'Internal server error', | |
docs: 'https://docs.myapp.io' | |
}, | |
{ | |
name: 'ENORESULT', | |
statusCode: 403, | |
type: 'api_error', | |
message: 'No results found', | |
docs: 'https://docs.myapp.io/queries' | |
} | |
]) | |
var app = merry() | |
app.router('GET', '/:username', function (req, res, ctx) { | |
db.users.get(ctx.params.username, function (err, body) { | |
if (err) return errors.EDATABASE(req, res, err) | |
}) | |
}) | |
app.router('GET', '/404', function (req, res, ctx) { | |
return res.end(errors.ENOTFOUND(req, res)) | |
}) | |
app.listen(env.PORT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment