Organelle for adding default response to incoming requests as expressjs middleware.
module.exports = function(){
return {
"GET": function(req, res, next) {
res.template = "landing"
next()
},
"POST": function(req, res, next) {
res.body = {success: true}
next()
},
"PUT": function(req, res, next) {
var not_implemented_error = new Error()
not_implemented_error.code = 400
not_implemented_error.body = "not implemented"
next(not_implemented_error)
}
}
}
// given the express app
var app = express()
// construct express response middleware instance
var plasma = new (require("organic-plasma"))()
require("organic-express-response")(plasma, {reactOn: "ExpressServer"})
// and attach it to express app
plasma.emit({type: "ExpressServer", data: app})
// respond with template
app.get("/", function(req, res, next){
res.template = "landing"
next()
})
// respond with raw json data
app.post("/data", function(req, res, next){
res.body = {success: true}
next()
})
// respond with custom error
app.get("/failing/route", function(req, res, next){
var errorFound = new Error()
errorFound.code = 400
errorFound.body = "missing argument"
next(errorFound)
})
The middleware
- intercepts all requests and sends them as response in case they define
response properties
- or responds with defaults if configured to do so
- or pass the control flow to followup middleware functions if configured to do so
Optionally the middleware
- intercepts errors/exceptions from the request - response chain and sends them as response in case they define
response error properties
- or pass the control flow to followup error middleware functions.
Does res.render(res.template)
Sends res.body
data either to json
or send
express res methods.
Sets res.status
Does res.render(err.template)
Sends err.body
data either to json
or send
express res methods.
Sets res.status
{
"source": "node_modules/organic-express-response",
"reactOn": "ExpressServer",
"skipErrorResponses": false,
"defaultNextRoute": undefined,
"skipDefaultResponse": true,
"defaultCode": 404,
"defaultTemplate": undefined,
"defaultBody": "not found",
"skipDefaultErrorResponse": true
"defaultErrorCode": 500,
"defaultErrorBody": "error found",
}
Should be either ExpressServer
chemical with expected structure or array of chemicals where the first one is mapped as ExpressServer
chemical.
All specify what is the default response if response properties
where not found. If defaultTemplate
is provided then it will be used instead of defaultBody
.
Optional, if set to true
default response will not be triggered and the middleware will call next(defaultNextRoute)
instead.
Optional, if set to true
will not send default error response
All specify what is the default error response code and data when error has been found but it is missing error response properties
Optional, if set to true
will not mount error middleware handler to express app leaving only the middleware for responses without error.