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');
});
Baz
  • 12,713
  • 38
  • 145
  • 268
  • 2
    Possible duplicate of [How to specify HTTP error code?](http://stackoverflow.com/questions/10563644/how-to-specify-http-error-code) – Dekel Oct 28 '16 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? – VLAZ Oct 28 '16 at 22:07

1 Answers1

1

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

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

Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128