Javascript Express模板未链接到脚本、css或图像
使用Express,我运行index.html文件,但无法使css、js或图像正确链接。没有图像显示,css和js没有链接Javascript Express模板未链接到脚本、css或图像,javascript,html,css,express,Javascript,Html,Css,Express,使用Express,我运行index.html文件,但无法使css、js或图像正确链接。没有图像显示,css和js没有链接 <link rel="stylesheet" type="text/css" href="css/main.css"> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/main.js
<link rel="stylesheet" type="text/css" href="css/main.css">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/main.js"></script>
img/logo.png
在app.js中:
var express = require('express');
var app = express();
var path = require('path');
app.get('/', function(req, res) {
//res.sendFile(path.join(__dirname + '/assets/templates/theme1/index.html'));
});
app.get('/about/', function(req, res) {
res.sendFile(path.join(__dirname + '/assets/templates/theme1/about.html'));
});
app.get('/services/', function(req, res) {
res.sendFile(path.join(__dirname + '/assets/templates/theme1/services.html'));
});
app.get('/services/', function(req, res) {
res.sendFile(path.join(__dirname + '/assets/templates/theme1/news.html'));
});
app.get('/contact/', function(req, res) {
res.sendFile(path.join(__dirname + '/assets/templates/theme1/contact.html'));
});
app.listen(3000);
需要更好地理解应用程序。获取和应用程序。使用以及res.sendFile
谢谢大家,它不起作用了,因为你没有告诉Express提供你想要的文件(js和css等等)
首先,要使用以下命令设置静态路由:
app.use(express.static(uu dirname+/assets))代码>
此时,“资产”目录中的所有内容都将相对于根URL提供服务。因此,/img/someimage.jpg
是它的正确URL 如何使用app.use设置app.js文件?我还需要app.get和app.sendfile吗?取决于。如果您想保持请求to/about返回文件系统上assets/templates/theme1/about.html文件的行为,那么您可能更容易继续执行您不做的操作,只需在所有app.get声明之前添加我建议的更改。
var express = require('express');
var app = express();
var path = require('path');
app.get('/', function(req, res) {
//res.sendFile(path.join(__dirname + '/assets/templates/theme1/index.html'));
});
app.get('/about/', function(req, res) {
res.sendFile(path.join(__dirname + '/assets/templates/theme1/about.html'));
});
app.get('/services/', function(req, res) {
res.sendFile(path.join(__dirname + '/assets/templates/theme1/services.html'));
});
app.get('/services/', function(req, res) {
res.sendFile(path.join(__dirname + '/assets/templates/theme1/news.html'));
});
app.get('/contact/', function(req, res) {
res.sendFile(path.join(__dirname + '/assets/templates/theme1/contact.html'));
});
app.listen(3000);