I am trying out node / express js and created a little web project.
I have the views in root /views directory
so:
root
/views
/css
I've added this to /views/index.html file:
<link rel="stylesheet" type="text/css" href="css/style.css" />
And this is my server.js file code:
var express = require("express");
var app = express();
var router = express.Router();
var path = __dirname + '/views/';
var path = __dirname + '/css/'; //Not working
router.use(function (req,res,next) {
console.log("/" + req.method);
next();
});
router.get("/",function(req,res){
res.sendFile(path + "index.html");
});
router.get("/about",function(req,res){
res.sendFile(path + "about.html");
});
router.get("/contact",function(req,res){
res.sendFile(path + "contact.html");
});
app.use("/",router);
app.use(express.static('public'));
app.use("*",function(req,res){
res.sendFile(path + "404.html");
});
app.listen(3000,function(){
console.log("Live at Port 3000 - http://localhost:3000");
});
How can I get it to read my css files?
app.use('/css', express.static('css'));. I see that you just copy-pasted key line from the docs page I linked to - you have to make sure it's pointing at the right path specific to your project.