【问题标题】:Node.js can't use external css fileNode.js 不能使用外部 css 文件
【发布时间】:2017-11-07 10:55:24
【问题描述】:

我最近开始学习 node.js,但遇到了问题。我使用 express 来访问我的公共文件,除了 css 文件外一切正常。我对该主题进行了一些研究,并使用了我发现的所有内容,但它不起作用。

我的文件夹结构

应用程序.js 酒馆 索引.html 样式.css

这是我的html:

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

还有我的 app.js 文件:

    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);

【问题讨论】:

  • 您是否尝试过使用 SASS 或 Less ?

标签: css node.js


【解决方案1】:

我认为你的问题出在这一行:

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

您将“/pub/public”设置为公用文件夹,您只需设置“/pub”

你可以试试这样的东西吗?

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-19
    • 2020-05-14
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多