0

I'm using express to serve the REST API endpoints for a mocked backend. For one of the endpoints, I'd like to be able to return different http response codes, with the other endpoints continuing to return 200. My code currently looks something like this:

var port = 32000;

var express = require("express");
var bodyParser = require("body-parser");
var app = express();

app.use( bodyParser.json() );
app.use( bodyParser.urlencoded({ extended: true }) );

var setHeaders = function(req, res) {
    res.setHeader("Content-Type", "application/json");
    res.setHeader("Access-Control-Allow-Origin", "http://localhost:2000");
};

app.get("*", setHeaders);

app.listen(port, function () {});

app.get("my/enpoint/one", function(req, res){
    res.send('hello');
});
2
  • 2
    Possible duplicate of How to specify HTTP error code? Commented Oct 28, 2016 at 22:06
  • Do you want for just one endpoint to return a different code/response to all the others, yet it's still the same response every time you call it, or do you want one endpoint to return different responses when you call it? Commented Oct 28, 2016 at 22:07

1 Answer 1

1

You can use res.status to set the HTTP code:

res.status(404).send('Bad Request');

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.