How to make express serve static files from another upper directory?

express.static not working
express serve html
express.static options
serving static files in express
node serve static files
node-static
express serve dist folder
express get file

If I have an nodejs express application with folowing folder structure:

-src - client - public - css - js - ... - views - server - server.js

  • How can I serve the static files in the public folder from the server.js file, since it is located above the index.js root location?
  • How should the:

app.use(express.static();

look like?

----UPDATE---

SOLVED this by using: app.use(express.static(path.join(__dirname, '/../client/public')));

Actually solved my problem by using:

app.use(express.static(path.join(__dirname, '/../client/public')));

How to make express serve static files from another upper directory?, To serve static files such as images, CSS files, and JavaScript files, use the The root argument specifies the root directory from which to serve static assets. To create a virtual path prefix (where the path does not actually exist in the file If you run the express app from another directory, it's safer to use the absolute path  To serve static files such as images, CSS files, and JavaScript files, use the express.static built-in middleware function in Express. The function signature is: The root argument specifies the root directory from which to serve static assets. For more information on the options argument, see express.static.

You can use path.join()

app.use(express.static(path.join(__dirname,'public')));

Serving static files in Express, Express makes serving static files ridiculously easy! New to Express? Checkout our article on the basics of Express to get caught up on the basics. some files public through our Express server by making a whole directory public. other routes to your Express app, as long as the route doesn't have the  app.use(express.static('public')); Note − Express looks up the files relative to the static directory, so the name of the static directory is not part of the URL. Note that the root route is now set to your public dir, so all static files you load will be considering public as root.

just do this, (as per your directory structure)

app.use(express.static(path.join(__dirname, 'src/client/public'))); 
// http://localhost:3000/hello.html

or

app.use('/static', express.static(path.join(__dirname, 'src/client/public')))
// http://localhost:3000/static/hello.html

Serving Static Files in Express.js ← Alligator.io, ExpressJS - Serving static files - Static files are files that clients download as they are from the server. Create a new directory, public. Note − Express looks up the files relative to the static directory, so the name of the static directory is not part  Express looks up the files relative to the static directory, so the name of the static directory is not part of the URL. It means you can access assets something like below shown URLs. See above URLs public is not part of the url; public directory act as the root directory.

ExpressJS - Serving static files, In this article, we are going to a build simple app to serve static files like HTML files, CSS Let's assume that the user visits the root path ( http://localhost:3000 ). Let's create another file, called server.js , and set the folder with our three HTML  In this section, you will learn how to serve static resources like images, css, JavaScript or other static files using Express.js and node-static module. Serve Static Resources using Express.js. It is easy to serve static files using built-in middleware in Express.js called express.static.

Serving Static Files with Node and Express.js, Learn how to serve static resources like images, css, JavaScript or other static files It is easy to serve static files using built-in middleware in Express.js called example serves static resources from the public folder under the root folder of  Create a new middleware function to serve files from within a given root directory. The file to serve will be determined by combining req.url with the provided root directory. When a file is not found, instead of sending a 404 response, this module will instead call next() to move on to the next middleware, allowing for stacking and fall-backs.

Serving Static Resources in Node.js, Setting up Express Server. Create a project folder and enter into the project directory by using the following command: mkdir express-static-app  Serving static files in Express documentation, or more detailed serve-static documentation, including the default behavior of serving index.html: By default this module will send “index.html” files in response to a request on a directory.

Comments
  • path.resolve(__dirname, '../client/public/') might also work . e.g. with res.sendFile instead of express.static .
  • why add duplicate answer?
  • The one he provided was incorrect and he editted it afterward
  • here the first edit app.use('/static', express.static(path.join(__dirname, 'public')))
  • I simply had a different opinion than yours initially and in terms of the answer , and I posted it , so perhaps others can decide. Have a good day !
  • you just add same answer
  • I think this doesn't work because __dirname refers to the directory server. The problem is that public is located above server and I can't just do app.use(express.static(path.join(__dirname, '../client/public')));