None
None

Reputation: 2433

How to send an existing object containing headers, statuscode and body?

I have a function that returns an object containing everything needed to reply to a request.

const response = {
  headers: {'Content-Type': 'image/svg+xml'},
  statusCode: 200,
  body: "<svg ...>"
};

app.get('*', function (req, res) {
  ?
})

How can I reply with the response object directly?

Upvotes: 0

Views: 34

Answers (1)

Tushar Tambe
Tushar Tambe

Reputation: 135

I think there is no direct way to do that. but you can write your own function that will do this for you. Like

const yourResponse = {
  headers: {'Content-Type': 'image/svg+xml'},
  statusCode: 200,
  body: "<svg ...>"
};

const sendResponse = function(req, res, yourResponse){
  res.set(yourResponse.headers);
  res.status(yourResponse.statusCode).send(yourResponse.body);
}

app.get('*', function (req, res) {
  sendResponse(req, res, response);
})

Upvotes: 1

Related Questions