0

How do I use question marks in my express project such as:

app.get("/", (request, response) => {
  response.sendFile(__dirname + "/views/index.html");
});


app.get("/?no-header", (request, response) => {
  response.sendFile(__dirname + "/views/noheader.html");
});
Janneck Lange
  • 882
  • 2
  • 11
  • 34
  • 2
    Query strings are not part of the route `path` that express uses, so in you're example both requests would be handled in one function. The query string values are available in the `request.query` object. – Matt Apr 02 '20 at 01:53

1 Answers1

0

Query strings are removed from the path before comparing to Express routes. So, both of the above URLs would go to the first request handler. If you want to separate out those two, you would do that in the same request handler by examining request.query:

app.get("/", (request, response) => {
  if (request.query.hasOwnProperty("no-header")) {
      // ?no-header is present
      response.sendFile(__dirname + "/views/noheader.html");
  } else {
      response.sendFile(__dirname + "/views/index.html");
  }
});
jfriend00
  • 683,504
  • 96
  • 985
  • 979