I need help setting up a server for my project using Express. Here is the structure of my file directory:
root-directory
├── public
│ ├── css
│ │ └── styles.css
│ ├── images
│ │ └── img1.png
| | └── img2.png
| | └── img3.png
│ └── index.html
| └── main.js
└── server
└── app.js
This is my current server code:
const express = require('express');
const path = require('path');
const app = express();
app.use('/static', express.static('public'));
app.use(express.json());
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../public/index.html'))
});
const port = 5000;
app.listen(port, () => console.log(`Server is listening on port ${port}.`))
Right now, only the static HTML file is being displayed by the server, and not the linked CSS or JavaScript files. I tried modifying the sendFile function to
res.sendFile(path.join(__dirname, '../public'))
, but I received an Error: Cannot GET /
.
How can I adjust my server configuration to showcase all HTML, CSS, and JavaScript content simultaneously?