Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS and Express : CSS not loading

Tags:

html

css

node.js

I have created my own server and router files to serve my html pages. The page renders, but my problem is, it renders without css styling.

I'm using express.static to serve my /public files, which in theory should serve the css file - or so I thought. I have done a Google search of my problem and looked at the links on the first couple of pages but I still cannot fix my problem. Can anyone sport my mistake?

My directory is:

  • NODEJS
    • node_modules
    • public
      • css
        • style.css
      • js
    • router
      • main.js
    • views
      • my html files

My server code is:

var express = require('express');
var app = express();
var fs = require('fs');
var url = require('url');
var path = require('path');
var http = require('http');
var router = express.Router();

var bodyParser = require('body-parser');
app.use(bodyParser());

app.use(express.static(__dirname + "/public", {maxAge: 3456700000})); 
require('./router/main')(app);   
app.engine('html', require('ejs').renderFile);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

var server = app.listen(3000, function () {

  var host = server.address().address
  var port = server.address().port

  console.log('Example app listening at http://%s:%s', host, port);

});

My Router code is:

var url = require('url');

module.exports = function (app) {

    app.get('/', function (req, res) {
        res.render('../views/default.html');
        console.log("Home page displayed");
    });

// Some more get requests for different pages

};

My html code is:

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<title>Index</title>
<link rel="stylesheet" type="text/css"  href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/css/style.css">       
</head> 

[error fixed: code needed re-arranging and chrome browser needed updating to most recent version]

like image 310
hturner Avatar asked Oct 24 '25 01:10

hturner


1 Answers

the css path looks wrong it should be /css/style.css instead of portalstyle.css, like this

<link rel="stylesheet" type="text/css" href="/css/style.css">       
like image 169
Yogesh Khatri Avatar answered Oct 25 '25 17:10

Yogesh Khatri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!