Я пытаюсь применить маску JSON (не относящуюся к игре или фильму в пятницу 13) на мой restify json ответ .
Обычный способ сделать это - вызвать next () на всех маршрутах сервера, а затем реализовать маску в обработчике pre для restify
I can 'сейчас, я собираюсь сделать быстрое исправление, поэтому я пытаюсь обезьяна исправить мой ответ restify следующим образом:
Создайте модуль 'CustomResponse':
function CustomResponse (originalResponse) {
this.restifyResponse = originalResponse
this.type = 'customResponse'
}
CustomResponse.prototype.send = function (code, payload) {
if (!payload) { payload = code }
console.log(payload)
this.restifyResponse.send(code, payload)
}
module.exports = CustomResponse
Iя пытаюсь использовать этот модуль в промежуточном программном обеспечении , но он не работает:
var CustomResponse = require('./customResponse') // the file above
server.use(function (req, response, next) {
response = new customResponse(response)
next()
})
Это работает, только когда я использую его в маршруте :
var CustomResponse = require('./customResponse') // the file above
server.get({
path: '/foo/bar'
}, function(request, response, next){
response = new CustomResponse(response)
response.send('baz') // this will print on the console the response and send it to the browser
})
Спасибо за любую подсказку!