Search code examples
javascriptnode.jsbackbone.jsexpress

unable to use multiple static paths for frontend and backend in expressjs


I am creating a nodejs/backbone application and wish to keep the directories for backend and frontend different. Here is my directory structure:

├── backend
│   ├── api
│   ├── file-uploads
│   └── ui
│       ├── assets
│       │   ├── css
│       │   ├── images
│       │   └── js
│       └── index.html
├── frontend
│   ├── assets
│   ├── index.html
│   └── modules
├── index.js
├── npm-debug.log
└── package.json

In the index.js file, i have the following code

app.use(express.static('/admin',__dirname + '/backend/ui/'));
app.use(express.static(__dirname + '/frontend/'));

Now the url / works fine and display the index.html file inside the /frontend directory but the url /admin doesn't work. I am expecting it to display the index.html file inside /backend/ui/ directory. Where I am going wrong here?


Solution

  • You'll want to pass the URL path, '/admin', to app.use([path], function) rather than express.static(root, [options]):

    app.use('/admin', express.static(__dirname + '/backend/ui/'));
    app.use(express.static(__dirname + '/frontend/'));
    

    The middleware only expects a single path, so it's currently attempting to serve files from the /admin directory on your harddrive.