【发布时间】: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 """, "'", "=", ">", "<", or "`", characters, must not be the empty string[...] -
@t.niese 他一个都没用过!!
标签: javascript node.js server