82

I'm trying to follow the instructions to https://stackoverflow.com/a/18633827/2063561, but I still can't get my styles.css to load.

From app.js

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

In my .ejs, I have tried both of these lines

<link rel="stylesheet" type="text/css" href="/css/style.css" />
<link rel="stylesheet" type="text/css" href="/public/css/style.css" />

Neither loads the css. I've gone into the developer's console noticed the type is set to 'text/html' instead of 'text/css'.

My path looks like

.
./app.js
./public
    /css
        /style.css
Community
  • 1
  • 1
stealthysnacks
  • 1,091
  • 1
  • 12
  • 16

12 Answers12

171

Use this in your server.js file

app.use(express.static(__dirname + '/public'));

and add css like

<link rel="stylesheet" type="text/css" href="css/style.css" />

dont need / before css like

<link rel="stylesheet" type="text/css" href="/css/style.css" />
Arunkumar
  • 5,150
  • 4
  • 28
  • 40
  • 18
    That'll break for non-root paths though (e.g. hitting `/article/edit` will result in the browser looking for `/article/css/style.css`). – mscdex Jul 05 '14 at 03:30
  • I used let path = path.join(__dirname + '/public'); ... you use let path = require('"path"); to use path.join ... joining them as strings on windows did not work – FernandoZ Sep 22 '17 at 14:46
  • 1
    [For more information](https://expressjs.com/en/starter/static-files.html) on serving static files (like stylesheets) using Express. – Dylan Landry Jul 25 '18 at 17:59
  • 7
    There is a mistake here, you should use the '/' before you path to your .css, otherwise for non-root paths it will not work because the path is not absolute : – Zgore Feb 27 '19 at 12:16
  • 1
    I came here looking how to get the images that i was setting as a background to be linked correctly. The same method below can be applied to an 'img' folder which can then be linked to normally if they are both in the public folder. Thanks for a great answer! – Jason R Jun 30 '20 at 07:16
  • @arunkumar I am new to express, I have a question why do we need to use middle wares like `static` to serve CSS files ? like before studying node and express, I just linked the CSS file to the HTML file and it works fine but not while using express. Why is that ? – The Chinky Sight Dec 25 '20 at 12:09
  • `app.use(express.static(path.join(__dirname, 'public')));` + `` to include it. In `public` folder there must be a folder `css` which resolves to final path `public/css`. – George Mylonas Nov 08 '21 at 19:36
36

1.Create a new folder named 'public' if none exists.

2.Create a new folder named 'css' under the newly created 'public' folder

3.create your css file under the public/css path

4.On your html link css i.e <link rel="stylesheet" type="text/css" href="/css/style.css">

// note the href uses a slash(/) before and you do not need to include the 'public'

5.On your app.js include : app.use(express.static('public'));

Boom.It works!!

Eric Nderitu
  • 481
  • 5
  • 7
  • The approach taken in the accepted answer is more reliable than this. See [this question](https://stackoverflow.com/questions/8131344/what-is-the-difference-between-dirname-and-in-node-js). We know the relationship between the public directory and the directory with the script in it. We don't know the relationship between it and the directory the script was invocked from. – Quentin Jul 30 '19 at 08:48
12

The custom style sheets that we have are static pages in our local file system. In order for server to serve static files, we have to use,

app.use(express.static("public"));

where,

public is a folder we have to create inside our root directory and it must have other folders like css, images.. etc

The directory structure would look like :

enter image description here

Then in your html file, refer to the style.css as

<link type="text/css" href="css/styles.css" rel="stylesheet">
2rahulsk
  • 488
  • 4
  • 10
10

For NodeJS I would get the file name from the res.url, write the header for the file by getting the extension of the file with path.extname, create a read stream for the file, and pipe the response.

const http = require('http');
const fs = require('fs');
const path = require('path');
const port = process.env.PORT || 3000;

const server = http.createServer((req, res) => {
    let filePath = path.join(
        __dirname,
        "public",
        req.url === "/" ? "index.html" : req.url
    );

    let extName = path.extname(filePath);
    let contentType = 'text/html';

    switch (extName) {
        case '.css':
            contentType = 'text/css';
            break;
        case '.js':
            contentType = 'text/javascript';
            break;
        case '.json':
            contentType = 'application/json';
            break;
        case '.png':
            contentType = 'image/png';
            break;
        case '.jpg':
            contentType = 'image/jpg';
            break;
    }

    console.log(`File path: ${filePath}`);
    console.log(`Content-Type: ${contentType}`)

    res.writeHead(200, {'Content-Type': contentType});

    const readStream = fs.createReadStream(filePath);
    readStream.pipe(res);
});

server.listen(port, (err) => {
    if (err) {
        console.log(`Error: ${err}`)
    } else {
        console.log(`Server listening at port ${port}...`);
    }
});
8

Use in your main .js file:

app.use('/css',express.static(__dirname +'/css'));

use in you main .html file:

<link rel="stylesheet" type="text/css" href="css/style.css" />

The reason you getting an error because you are using a comma instead of a concat + after __dirname.

Sylhare
  • 5,907
  • 8
  • 64
  • 80
White Lantern
  • 89
  • 1
  • 2
4

In your app or server.js file include this line:

app.use(express.static('public'));

In your index.ejs, following line will help you:

<link rel="stylesheet" type="text/css" href="/css/style.css" />

I hope this helps, it did for me!

Niso
  • 79
  • 10
3

I have used the following steps to resolve this problem

  1. create new folder (static) and move all js and css file into this folder.
  2. then add app.use('/static', express.static('static'))
  3. add css like <link rel="stylesheet" type="text/css" href="/static/style.css"/>
  4. restart server to view impact after changes.
Paolo
  • 20,112
  • 21
  • 72
  • 113
sonu singh
  • 39
  • 4
3

IMHO answering this question with the use of ExpressJS is to give a superficial answer. I am going to answer the best I can with out the use of any frameworks or modules. The reason this question is often answerd with the use of a framework is becuase it takes away the requirment of understanding 'Hypertext-Transfer-Protocall'.

  1. The first thing that should be pointed out is that this is more a problem surrounding "Hypertext-Transfer-Protocol" than it is Javascript. When request are made the url is sent, aswell as the content-type that is expected.
  2. The second thing to understand is where request come from. Iitialy a person will request a HTML document, but depending on what is written inside the document, the document itsself might make requests of the server, such as: Images, stylesheets and more. This question refers to CSS so we will keep our focus there. In a tag that links a CSS file to an HTML file there are 3 properties. rel="stylesheet" type="text/css" and href="http://localhost/..." for this example we are going to focus on type and href. Type sends a request to the server that lets the server know it is requesting 'text/css', and 'href' is telling it where the request is being made too.

so with that pointed out we now know what information is being sent to the server now we can now seperate css request from html request on our serverside using a bit of javascript.

var http = require('http');
var url = require('url');
var fs = require('fs');




    function onRequest(request, response){  
        if(request.headers.accept.split(',')[0] == 'text/css') {
             console.log('TRUE');

             fs.readFile('index.css', (err, data)=>{
                 response.writeHeader(200, {'Content-Type': 'text/css'});
                 response.write(data);
                 response.end();
             });  
        }

        else {
            console.log('FALSE');    

            fs.readFile('index.html', function(err, data){
                response.writeHead(200, {'Content_type': 'text/html'});
                response.write(data);
                response.end();
            });
        };
    };

    http.createServer(onRequest).listen(8888);
    console.log('[SERVER] - Started!');


Here is a quick sample of one way I might seperate request. Now remember this is a quick example that would typically be split accross severfiles, some of which would have functions as dependancys to others, but for the sack of 'all in a nutshell' this is the best I could do. I tested it and it worked. Remember that index.css and index.html can be swapped with any html/css files you want.

JΛYDΞV
  • 8,532
  • 3
  • 51
  • 77
  • I don't understand why you would write an answer in pure node/JavaScript - the question is specifically about express and how to serve files properly using that library. What you say about the basis of the problem being about HTTP is also wrong, it has nothing to do with separating files. Even if the OP followed your advice they still wouldn't know how to serve files on express which is what they wanted to do – Scott Anderson Aug 02 '20 at 20:34
  • @ScottAnderson first of all this question has been edited from the original question, but I would agree that the question as it looks in its contemporary state is directed towards Express, through the author never explicitly states it. When writing this I was hoping to help give an understanding of the mechanics under the hood. I feel that is more help than just giving a couple lines of code to person that's obviously very new to Node. If you don't like it down vote it. If the question hits 0 I will remove it. Every JS framework, no matter the flavor, in essence is Vanilla Flavored. – JΛYDΞV Aug 03 '20 at 18:11
  • The question has a code line right up the top which talks about app.set (maybe this wasn't there originally). Don't get me wrong I think that understanding this is useful for web programming, but I just don't see how it's relevant to this question given what OP wrote. Anyways after commenting I saw someone answered very similarly: whilst I can't verify I could say that maybe the original question was quite different. If not, however, then IMHO this falls under 'useful knowledge', not 'relevant information' and should be formalised slightly into a blog or gist and linked in the comments only – Scott Anderson Aug 04 '20 at 08:55
1

Use this in your server.js file

app.use(express.static('public'));

without the directory ( __dirname ) and then within your project folder create a new file and name it public then put all your static files inside it

Kqt
  • 21
  • 1
  • 1
    incomplete answer. – Nandit Mehra Jun 23 '19 at 20:46
  • The approach taken in the accepted answer is more reliable than this. See [this question](https://stackoverflow.com/questions/8131344/what-is-the-difference-between-dirname-and-in-node-js). We know the relationship between the public directory and the directory with the script in it. We don't know the relationship between it and the directory the script was invocked from. – Quentin Jul 30 '19 at 08:49
1

Its simple if you are using express.static(__dirname + 'public') then don't forget to put a forward slash before public that is express.static(__dirname + '/public') or use express.static('public') its also going to work; and don't change anything in CSS linking.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Dhananjaya
  • 33
  • 8
1

the order of registering routes is important . register 404 routes after static files.

correct order:

app.use("/admin", admin);
...

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

app.use((req, res) => {
  res.status(404);
  res.send("404");
});

otherwise everything which is not in routes , like css files etc.. , will become 404 .

-2

The above responses half worked and I'm not why they didn't on my machine but I had to do the following for it work.

  1. Created a directory at the root

    /public/js/

  2. Paste this into your server.js file with name matching the name of directory created above. Note adding /public as the first param

    app.use('/public',express.static('public'));

  3. Finally in the HTML page to which to import the javascript file into,

    <script src="public/js/bundle.js"></script>

devyJava
  • 135
  • 8