0

I recently start learning node.js, and I got problem. I use express to acces my public file, everything work fine except css files. I did some research on the topic, and use everthing i found, but it dose not work.

My folder structure

    app.js
    pub
     index.html
     style.css

This is my html:

	<!DOCTYPE html>
	<html > 
	<head>
	  <title> Curriculum Vitae </title>
		<meta charset="UTF-8"> 
	  <link rel="stylesheet" href="style.css"/>
	</head>
      <body>
       ...
	  </body>
	</html>

And my app.js file:

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


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

http.createServer(function (req, res) {
  var q = url.parse(req.url, true);
  var filename = "." + q.pathname;
  fs.readFile(filename, function(err, data) {
    if (err) {
      res.writeHead(404, {'Content-Type': 'text/html'});
      return res.end("404 Not Found");
    }  
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    return res.end();
  });
}).listen(8080);
1
  • have you tried using SASS or Less ? Commented Nov 7, 2017 at 11:13

1 Answer 1

4

I think your problem is on this line :

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

You are setting "/pub/public" as public folder, and you just need to set "/pub"

Can you try with something like this ?

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

Hope it helps.

Sign up to request clarification or add additional context in comments.

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.