为什么取消CSS文件?
我正在做一个简单的节点项目来练习,但我不能设法用它的CSS样式提供html文件。之前我在问过同样的问题,但我没有提到css文件的状态在开发者工具中被“取消”了。当我编辑它没有人回答为什么它被取消,所以我希望有人帮助我。这让我发疯了。这是我的app.js代码:app.use(“/ signup”,express.static(“public”));
app.get("/", (req, res)=>{
res.send("Welcome to our website");
});
app.get("/signup", (req, res)=>{
res.sendFile(`${__dirname}/index.html`)
})
这是我的HTML代码:
<head>
<title>testapp</title>
<link href="./public/styles.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<form method="post" action="/signup">
<input type="text" placeholder="*First Name" required></input>
<input type="text" placeholder="*Last Name" required></input>
<input type="number" placeholder="*Age" required></input>
<input type="email" placeholder="*Email" required></input>
<input type="submit" value="Submit" required></input>
</form>
</body>
当我删除“/ signup”的get方法,并将我的index.html放在公共目录中同时更新到css的链接时,它工作得很好,但我不希望它在'/'路由中。而且在没有localhost的情况下打开index.html文件时,它可以正常工作。我搜索了一下,发现有人问过同样的问题,我认为答案在我的情况下是不对的,我不确定,我也不理解.What does status=canceled for a resource mean in Chrome Developer Tools?。
先感谢您。
回答如下:阅读有关静态服务的文档:https://expressjs/en/starter/static-files.html
您不需要在css链接中指定/public
;只需执行以下操作:
<link href="/styles.css" type="text/css" rel="stylesheet"/>