Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Landing page keeps returning static files in express

I am trying to use static files in my project, but when the user makes a get request to my "/" landing page. I want to send non-static things like a json. But for some reason it just automatically sends my index.html in my static file.

const express = require("express");
const app = express();

app.use(express.static("public"));

app.get("/", (req, res) => {   //This sends the user to the index.html even tho i want to send 123
  res.send("123");
});

app.listen("3000");
like image 239
Vehbi Karaağaç Avatar asked Nov 18 '25 03:11

Vehbi Karaağaç


1 Answers

As your code is written, the express.static() middleware looks at the / request, finds an index.html file in the public directory and serves that as the response for the / request. This is a feature of express.static() that is causing a conflict for you with your custom route for "/".

You have at least four choices for a solution here:

  1. You can specify an option for express.static() to disable the index.html feature so it will avoid the "/" route and pass control on to your subsequent route handlers.
  2. You can move the express.static() middleware AFTER your app.get("/", ...) route so that your custom route gets first dibs at the request.
  3. You can remove the index.html file from your public directory so express.static() won't find it. Using a template system for all your HTML files that locates all HTML template files in some other directory that express.static() can't see (such as a views directory) would cause this to happen too or just moving it to some private directory and using it from your code from the private directory would work too.
  4. Give ALL your static resources a common path prefix so there is never a URL conflict between static resources and custom route handlers.

The first option (disable index.html feature in express.static()) would look like this:

const express = require("express");
const app = express();

// disable index.html feature
app.use(express.static("public"), {index: false});

app.get("/", (req, res) => {
  res.send("123");
});

app.listen("3000");

The second option (change the order of route definitions/middleware) would look like this:

const express = require("express");
const app = express();

app.get("/", (req, res) => {
  res.send("123");
});

// put this after custom route definitions so they take precendence
app.use(express.static("public"));

app.listen("3000");

The fourth option (give all static resources a common path prefix) would look like this:

const express = require("express");
const app = express();

app.use(express.static("/static", "public"));

app.get("/", (req, res) => {
  res.send("123");
});

app.listen("3000");

With this fourth option, you'd then have to prefix all your static URLs with the static prefix (which you can make anything you want) and thus "/" would never be a match for express.static().

like image 78
jfriend00 Avatar answered Nov 19 '25 16:11

jfriend00