I am attempting to change the response sent by express but have been unsuccessful. I have tried defining the following middleware in my server.js but I believe this is not being called because the default platform middlewares are defined ahead of mine?
const express = require('express')
const Router = express.Router;
const eventsRouter = new Router();
const App = express();
function customEventHandler(req, res, next) {
console.log('Custom Event Handler');
if (req.headers.hasOwnProperty('HTTP_VALIDATION_TOKEN') && req.headers.HTTP_VALIDATION_TOKEN.length > 0){
res.header('Validation-Token', req.headers.HTTP_VALIDATION_TOKEN);
res.status(200);
res.send();
}
}
eventsRouter.post('/event/hook/:product', customEventHandler);
App.use(eventsRouter);
I believe that this solution is on the right track but I am not sure how I can modify for my needs. Can I use this idea to extend express. response so that my desired header is inserted regardless of the middleware handling the response?
Edit: I've also attempted by extending res.send in the following way but still no luck. Thanks for any suggestions.
const express = require('express')
const App = express();
App.use(function (req, res) {
var _send = res.send
res.send = function (data) {
console.log('log method on data', data)
if (req.headers.hasOwnProperty('HTTP_VALIDATION_TOKEN') && req.headers.HTTP_VALIDATION_TOKEN.length > 0) {
res.append('Validation-Token', this.req.headers.HTTP_VALIDATION_TOKEN);
}
// Call Original function
_send.call(res, data)
}});