Created
January 23, 2013 17:48
-
-
Save wookets/4610946 to your computer and use it in GitHub Desktop.
Add jsend to express.js response object
This file contains 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
require './lib/jsend' |
This file contains 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
# adds jsend methods to the express response object | |
express = require 'express' | |
res = express.response | |
res.jerror = (code, message) -> | |
if code instanceof Error | |
if code.message and code.message.indexOf(' :: ') isnt -1 # if we can split by :: then use that for name and message | |
codeAndMessage = code.message.split(' :: ') | |
code = codeAndMessage[0] | |
message = codeAndMessage[1] | |
else # standard error message | |
message = code.message | |
code = code.name | |
this.send({status: 'error', code: code, message: message}) | |
res.jsend = (result) -> | |
this.send({status: 'success', data: result}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment