Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

251
Visualizações
How to post-process JSON response in express?

I am building an API with express and JSON middleware. I want to have a generic middleware that wraps each JSON response. The wrapping is an asynchronous process - there is a backend process that asynchronously generates a signature for the given JSON object. Example:

A route:

app.use("/", (req, res) => res.json({status: "ok"}))

The middleware would ideally intercept the JSON response, asynchronously generate a signature and ultimately send the user the following JSON:

{
    "signature": "...",
    "content": { "status": "ok" }
}

What's the best approach to building such a middleware?

I have tried replacing res.json with a custom function. The problem with this solution is that the signature generation is asynchronous, but the res.json is a synchronous function. This was my approach:

export function signatureMiddleware(req, res, next) {
    var json = res.json;
    res.json = async function (obj) {
      json.call(this, await wrapAndSign(obj));
    };
    next();
}
about 4 years ago · Santiago Gelvez
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda