Automate express response function (res.send())

Asked

Viewed 105 times

1

I have an express api and would like to develop a function to standardize the customer response.

Code (running the function):

response(200, "Evento adicionado com sucesso!", res)

Scope of function:

function response(code, info, res) {

switch (code) {
    case 200:
        error = false
    case 201:
        error = false
    case 404:
        error = true
    case 401:
        error = true
    case 500:
        error = true
}
res.status(code)
return res.send({ error: error, info: info })

}
module.exports = {
    response: response
}

The problem

Not giving the answer to the customer. I tested with console.log() in the client and does not display the response. The request is made normally. What may be happening?

  • 3

    Has not break in their case...

  • 1

    kkkkkkk, Bah really forgot bro! It’s that kind has some express functions that can not use break, so did not put. But thanks !

  • 3

    And I only need two assignments, one for false and another to true, peesquise on the case and understand how to use it.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.