0

So I've been trying to figure this out for a while now and the answers that I have found are just confusing, vague without explanation and or, do not work - (At least for me).

First here is my project structure.

.. denotes a folder
- denotes a file
while four | denote a sub directory or file in parent folder

..public
|||| ..html&css
|||| |||| ..main
|||| |||| |||| - main.html
|||| |||| |||| - main.css
|||| ..javascript
|||| |||| -server.js

Hopefully the above project structure is understandable, Stack Overflows formatting is tough to work with.

Here is my code - server.js

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 seen a few ways to send the files when someone connects to the server, and have managed to get html to send but never both html and css. From researching I've seen a few ways to do this but the way in which they are explained is confusing and no answers I found explain in detail how to accomplish my question along with what and why your doing the things they are saying to accomplish it.

I have just been getting a lot about routes, static, app.get this and app.get that, res.sendFile and some other mumbojumbo but not any of it is really making sense. It also doesn't help that most of the answers don't have the persons complete project structure. If they did understanding which routes were doing what would be much easier.

This link seemed to be the best answer I came across but because of lack of project structure I cannot implement it into my own project and thus understand how it works.

If someone could even just explain how that works with a project structure implemented in their answer I would be grateful, otherwise if they have another way of doing it through the use of the fs module and express or something else.

I hope my question is understandable clear. Thank you for your time.

Trooper Z
  • 1,617
  • 14
  • 31
C.Gadd
  • 402
  • 3
  • 11
  • What do you mean cannot send css? with `express.static(..)` you can host any file. The html will be hosted at `localhost:8080/main/main.html` and the css at `localhost:8080/main/main.css` – Aritra Chakraborty Aug 02 '18 at 17:29

2 Answers2

1

I am going to explain how it works. But the code you have written is correct. Even I ran the code.

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}`);

Now, what express.static do? it exposes the particular folder and makes auto routes.
If you want to access main.css it will be hosted on localhost:8080/main/main.css In the main.html to use that CSS you just add a link.

<link rel="stylesheet" href="main.css"> or <link rel="stylesheet" href="/main/main.css">

But, you cannot HTTP GET let's say localhost:8080/main that will not work.

So, you need to run the server by typing node javascript/server.js and it will run just fine. But as the server.js is a backend code do not put it in the public folder. Maybe make a folder called src and put it there.

Aritra Chakraborty
  • 12,123
  • 3
  • 26
  • 35
  • This answer does have helpful information in it but my overall confusing question is still unanswered. NoteI have no problem in running the server and connecting. Your answer along with the first answer to this post helped me understand a lot of what I was uncertain about however my main goal was to successfully send a html file along with attached css to a client when they connect and then render it in their browser. Ive manged to get the html to send using express and a few other ways but the css is a problem. My above comments to the first answer explains in more detail. – C.Gadd Aug 03 '18 at 09:20
  • Also I already had the css/stylesheet linked to the html a as you have suggested, but it seems to do nothing. There is something else im missing for it to be sent to the client along with said html file. – C.Gadd Aug 03 '18 at 09:23
0

Let's look a a few things.

First, folder structure:

According to your setup, you have your server.js file embedded within the public folder, albeit one level down.

If you think of an MVC framework, see this sample tutorial, you want to put your server at the root of your application (not inside of the JavaScript folder inside public where you would serve any client-side javascript.

Second, regarding middleware:

When you call the express.static middleware, you will want to use the path module to create an absolute rather than relative path to the public folder, which is especially important for deployment.

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

Now, you will be able to access all the files in your public folder like so:

http://localhost:8080/main/*
http://localhost:8080/javascript/*

Of course * refers to whatever files are there.

Third, regarding app.listen:

The callback to app.listen is expecting to be returned a server object. See the documentation. Rather than setting up a listener on a route here, you should establish that outside of the app.listen call.

For instance, you may want to set up a basic get route to serve your main.html page on /. That's up to you. The express.static middleware is already serving this page as /main/main.html.

app.get('/', function (req, res) {
    res.sendFile(path.join(__dirname, 'public', 'main.html'));
});

let server = app.listen(8080);
wlh
  • 3,426
  • 1
  • 16
  • 32
  • This question helped a lot, and the tutorial using the MVC framework seems to contain a trove of info. Although not all of it makes sense I will follow it as best I can, thank you for that. Ive followed your instructions using the path module for an absolute path which makes sense and is understandable why I would use it. – C.Gadd Aug 03 '18 at 09:04
  • One thing thing though is that the main aspect of my overall question is how to have the css be sent with the html and rendered as a user connects, it doesn't seem to send automatically just because of a stylesheet being linked. In app.get function doing a res.sendFile for both the html and css doesn't work either. From what I've garnered from your answer it seems to be something to do with the routes. The html sends fine but I still have little idea how to send the css with it. Would I do two app.gets for the requested link or something else? The two app.gets doesn't seem to work either. – C.Gadd Aug 03 '18 at 09:11
  • The CSS is served by the `express.static` middleware. This allows you to add a `` to it within your HTML file. – wlh Aug 03 '18 at 13:40
  • Ok, thank you, you've been a great help. I now understand much more than I had originally and have been pointed in the right direction. With you answer and explanations I feel confident that I can figure out any other problems that come along. Finally thank you for the link to that sample tutorial. While it looks a bit advanced it still seems very interesting. I will use it as a learning curve to become more experienced with express. peace man..:) – C.Gadd Aug 03 '18 at 21:05