表示静态文件无法正常工作
我正在做一个简单的节点项目来练习,但我不能设法用它的CSS样式提供html文件。
我相信它之前使用相同的代码对我来说很好,但现在我不明白它为什么不运行。我搜索了它并复制了一些替换目录名称的代码,但它没有改变任何东西。这是我的代码。我还尝试使用路径模块来加入文件名和目录名。
app.use(express.static('public'));
app.get('/', (req, res)=>{
res.send("Welcome to our website");
});
app.get("/signup", (req, res)=>{
res.sendFile(__dirname + "/index.html");
});
//My directory:
testapp1
--node_modules
--public
--styles.css
--index.html
--app.js
--package.json
--package-lock.json
在开发者控制台的网络选项卡中,它表示:状态:已取消的类型:stylesheet initiator:index.html大小:0B时间:29ms瀑布:“没什么”
回答如下:app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function (req, res, next) {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
试试吧。