I have been struggling to solve this issue for quite some time now. The answers I have come across have been confusing, lacking explanation, or simply not working for me.
Let me provide you with an overview of my project structure.
.. indicates a folder
- indicates a file
while four | indicate a subdirectory or file within a parent folder
..public
|||| ..html&css
|||| |||| ..main
|||| |||| |||| - main.html
|||| |||| |||| - main.css
|||| ..javascript
|||| |||| -server.js
Hopefully, the above description of my project structure is clear. Formatting on Stack Overflow can be tricky to work with.
Now, let me share the code from my server.js file:
let express = require('express');
let app = express();
app.use(express.static('../html&css'));
let server = app.listen(8080, function () {
app.get(function (req, res) {
res.sendFile();
});
});
let port = server.address().port;
console.log(`Express app listening at ${port}`);
I have encountered various methods to send files when a user connects to the server. However, I have only been able to successfully send HTML and not CSS. While researching solutions, I have found explanations to be confusing and lacking in detail regarding how to achieve my specific goal.
Terms like routes, static, app.get, res.sendFile
, and other technical jargon are being used without clear explanation. It would be helpful if the responses included the complete project structure so that understanding the functioning of different routes becomes easier.
I stumbled upon this link, which seemed to offer a potential solution. However, due to the absence of a project structure, implementing it in my own project has proven challenging.
If someone could kindly clarify how this solution works with an implemented project structure, I would greatly appreciate it. Alternatively, if there is another approach involving the use of the fs
module in conjunction with Express or any other method, please do share.
I hope my question is clear and understandable. Thank you for your assistance.