【发布时间】:2025-12-20 05:55:06
【问题描述】:
我的 node.js 服务器上有以下代码(我正在使用 express):
app.get('/appointment/:id',function(req,res){
res.writeHead(200,{'Content-Type':'text/html'});
res.write('<link href="public/css/style.css" rel="stylesheet" type="text/css">');
res.write('<form name="accept" action="http://localhost:8080/appointment/'+ req.params.id+'/1" method="post">');
res.write('<input id="accept" type="submit" value="Accept">');
res.write('</form><br>');
res.write('<form name="decline" action="http://localhost:8080/appointment/'+ req.params.id+'/0" method="post">');
res.write('<input id="decline" type="submit" value="Decline">');
res.write('</form><br>');
res.end();
});
在我的根文件夹中,我有文件夹约会/public/css/style.css。
当我打开网页时,它只显示 2 个表单按钮,但没有应用 CSS。
CSS代码是:
#accept {
width:50px;
height:30px;
background-color:#00FF00;
color:#FFFFFF;
font-weight:bold;
font-size:120%;
}
#decline {
width:50px;
height:30px;
background-color:#FF0000;
color:#FFFFFF;
font-weight:bold;
font-size:120%;
}
有什么问题,我该如何解决?
编辑:层次结构如下:
-server_files
--nodeServer.js
--公共
---css
----style.css
【问题讨论】:
-
你能在另一个标签页中加载 CSS 文件吗?
-
据我了解您使用的是快递,所以您需要将此行添加到您的代码
app.use(express.static(__dirname + '/public'));,并在public/css/style.css之前添加/ -
@RienNeVaPlu͢s 不,我不是。当我单击链接时,它说它无法执行 GET 请求。我是否需要添加 app.get 并“提供”CSS 文件?
-
是的,您需要像 Alexander 所描述的那样提供静态文件。祝你好运:)
-
@remaker 如果
/public是您的静态根目录,则http://localhost/foo.css映射到文件public/foo.css。 (静态根目录的名称永远不会出现在静态 HTTP 路径中;文件系统的public成为服务器的/(根))您需要在<link>中使用/css/style.css。