我有这个文件来运行app.js
var express = require('express')
var app = express();
var router = express.Router();
var path = __dirname + '/views/';
router.get("/",function(req,res){
res.sendFile(path + "index.html");
});
app.use("/",router);
app.use("*",function(req,res){
res.sendFile(path + "index.html");
});
app.listen(8080,function(){
console.log("Live at Port 8080");
});
我在“views”文件夹中有“index.html”。 我想将本地 css 文件导入“css”文件夹中。 我尝试像这样调用该文件
<link href="css/main.css">
或
<link href="../css/main.css">
但是没有成功。 有没有具体的获取方法?
谢谢
最佳答案
如果我正确理解问题,您似乎没有为静态文件设置文件夹。例如:
app.use(express.static(path.join(__dirname, 'static')));
制作完成后,它应该开始寻找其中的 css。例如
static/css/main.css
可与 <link href="css/main.css">
一起使用
关于javascript - Node .js。如何从 html 调用本地文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47673225/