【问题标题】:Why isn't my public folder fetching files for '/' URL in my NodeJS App?为什么我的公用文件夹没有在我的 NodeJS 应用程序中获取“/”URL 的文件?
【发布时间】:2016-02-29 15:09:28
【问题描述】:

我正在尝试将静态文件放在我的公共文件夹中,例如 html、css 和 js 文件,但它们不会获取到 get url ('/')。 这是我的文件夹结构

app.js
node_modules
--express
package.json
public
--style.css

我的应用是:

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

var port = process.env.PORT || 3000;

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

app.get('/', function(req, res){
    res.send('<html><head><link href=assets/style.css type=text/css rel=stylesheet/></head><body><h1>Hello World</h1></body></html>');
});

app.get('/person/:id', function(req, res){
    res.send('<html><head></head><body><h1>Person: ' + req.params.id + '</h1></body></html>');
});

app.get('/api', function(req, res){
    res.json({ firstname: 'John', lastname: 'Doe' });
});

app.listen(port);

样式代码为:

 style.css:
 body {
       font-family: Arial, Helvetica, sans-sarif;   
}

【问题讨论】:

  • 你的 HTML 中的assets/ 是从哪里来的?
  • 你的问题不清楚,but they won't fetch to the get url ('/')是什么意思。你的意思是如果你把assets/style.css 加上你的主机前缀到你的浏览器的地址栏然后你看不到你的css文件的内容吗?而是向您显示什么消息?
  • @t.niese 他忘了用双引号,这样浏览器就不会加载 CSS 文件了。
  • @NidhinDavid 引号(您可以使用" 或')在此给定示例中是可选的,因为该属性的值不包含任何需要引用的字符。 W3C: 4.4. Attributes[...]an unquoted attribute value has the following restrictions: must not contain any literal space characters, must not contain any """, "'", "=", "&gt;", "&lt;", or "`", characters, must not be the empty string[...]
  • @t.niese 他一个都没用过!!

标签: javascript node.js server


【解决方案1】:

你的问题是,因为

<link href=assets/style.css type=text/css rel=stylesheet/>

等于

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

如果您不使用引号,则该值由以下字符之一终止:"、'、=、&gt;、&lt; 或 `(请参阅 @987654321 @ 更多细节)。至此,您的值由&gt; 终止,值为stylesheet/。

因为你的link 的rel 是stylesheet/ 而不是stylesheet,这就是为什么没有加载样式表的原因。

所以要么你必须在stylesheet 和/ 之间添加一个空格:

<link href=assets/style.css type=text/css rel=stylesheet />

或者你需要使用引号。

【讨论】:

  • 这解释了这个错误。干得好
【解决方案2】:

能否将“public”重命名为“assets”,然后使用...

app.use(express.static('assets'));

【讨论】:

    【解决方案3】:

    链接到 CSS 文件的 HTML 语句中存在语法错误。您忘记将值括在双引号中。

    请使用以下格式:

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

    如果您使用的是支持 Node.JS 的 ES6,那么您可以使用反引号(``)来更好地格式化内联 HTML 代码,例如:

    app.get('/', function(req, res){
        res.send(`
            <html>
            <head>
            <link rel="stylesheet" type="text/css" href="assets/style.css">
            </head>
            <body>
            <h1>Hello World</h1>
            </body>
            </html>`
            );
    });
    

    【讨论】:

    • 在这种情况下它们是可选的:W3C: 4.4. Attributes[...]an unquoted attribute value has the following restrictions: must not contain any literal space characters, must not contain any """, "'", "=", "&gt;", "&lt;", or "`", characters, must not be the empty string[...]
    猜你喜欢
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 2020-01-02
    相关资源
    最近更新 更多