0

I want to access my .html with localhost via node.js. I use the express framework for this. My .html contains some CSS, but this doesn't get loaded with the html file.

Code:

var express = require('express');
var app = express();

app.use(express.static('public'));
app.engine('.html', require('ejs').renderFile);

app.get('/', function(req, res) {
    res.render('FrontPage.html');
});

app.listen(3000, function(){
    console.log("listening to 3000");
})

HTML:

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



<body style="background-image:url(./img/bg.jpg)">

<div id="header">
        <a href="./frontPage.html"><img src="./img/Logo.png" height="5%" width="5%" alt="logo"></a>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
        <script src="./script/FrontPageJS.js"></script>
</div>

<div id="buttonWrapper">
    <div id="first" class="first">
        This is my first button
    </div>

    <div id="second" class="second">
        This is my second button
    </div>
</div>

CSS:

div#header{
    text-align: center;
}
div#buttonWrapper{
    text-align: center;
}
div.madeBefore, div.madeNotBefore{
    border-radius: 25px;
    background: -webkit-linear-gradient(left top, #ffc300 , #ff8300);
    background: -o-linear-gradient(bottom right, #ffc300, #ff8300);
    background: -moz-linear-gradient(bottom right, #ffc300, #ff8300);
    background: linear-gradient(to bottom right, #ffc300 , #ff8300);
    width: 500px;
    height: 425px;
    margin-right: 50px;

    padding:50px;
    padding-top: 250px;
    padding-bottom: 0px;

    display: inline-block;
    vertical-align: top;

    text-shadow: 0 2px 3px rgba(255, 255, 255, 0.3), 0 -1px 2px rgba(0, 0,       0, 0.2);
    color: #B36103;
    font-size: 60px;
    text-align: center;
}
div.madeNotBefore{
    margin-right: 0px;
}
div.madeBefore:hover, div.madeNotBefore:hover{
    background: -webkit-linear-gradient(left top, #ff7600 , #e96c00);
    background: -o-linear-gradient(bottom right, #ff7600, #e96c00);
    background: -moz-linear-gradient(bottom right, #ff7600, #e96c00);
    background: linear-gradient(to bottom right, #ff7600 , #e96c00);
}

How do I make sure the CSS gets sended togheter with the .html?

1
  • 2
    Show your html and css please Commented Dec 3, 2015 at 18:06

3 Answers 3

6

Add this:

var path = require('path');

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

create folder public, put css files inside and in your html file:

<link rel="stylesheet" type="text/css" href="/FrontPageCSS.css" />
Sign up to request clarification or add additional context in comments.

6 Comments

I still only get the html without any CSS.
Open your browser then go to the console (ctrl + shift + j in chrome) and look at "Console tab" Do you have any errors? Problem with load css?
Yes, it indeed gives me: failed to load resources. The path it gives me is still my old path instead of the new one with public/..
Try hard refresh Ctrl + F5?
Doesn't work either. I don't no why it still talks about the previous path in the error while I changed it in my html file
|
1

You need to make sure your css content is inside your public folder. For example if you have a folder css inside public folder and your css file is named FrontPageCSS.css then you can use it like this

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

You can asume that you are inside public for references.

Comments

1

Before doing anything:

1) Verify that public/ is in the main folder of the server. (So wherever your server file is, public/ should in that same directory)

2) ...verify the css file is actually there.

Then, use this to make the /public directory static:

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

And in your file, set this to get your CSS:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.