Express模板未链接到脚本,css或图像

时间:2016-07-20 02:27:17

标签: javascript html css express

使用Express,我运行index.html文件,但无法正确链接css,js或图像。没有图像显示,css和js没有链接。

root
    app.js
    package.json
    node_modules
    assets
        img
        css
        js
        templates
            theme1
                css
                fonts
                img
                js
                index.html
                about.html
                services.html
                news.html
                contact.html

我有以下目录结构:

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);

在app.js中:

app.get

需要更好地了解app.useres.sendFile以及import matplotlib.pyplot as plt import matplotlib.patches as patches fig = plt.figure(1) # create rectangles for the background upper_bg = patches.Rectangle((0, 0.5), width=1, height=0.5, transform=fig.transFigure, # use figure coordinates facecolor='gray', # define color edgecolor='none', # remove edges zorder=0) # send it to the background lower_bg = patches.Rectangle((0, 0), width=1.0, height=0.5, transform=fig.transFigure, # use figure coordinates facecolor='white', # define color edgecolor='none', # remove edges zorder=0) # send it to the background # add rectangles to the figure fig.patches.extend([upper_bg, lower_bg]) # create subplots as usual fig.add_subplot(221) fig.add_subplot(222) fig.add_subplot(223) fig.add_subplot(224) plt.show()

全部谢谢

1 个答案:

答案 0 :(得分:0)

所以,它不起作用,因为你并没有告诉Express提供你想要的文件(js和css等)。

首先,您要使用static middleware设置静态路由:

window.onresize=arrows; window.onload=arrows; function arrows(){ if(document.body.scrollHeight > window.innerHeight){ document.getElementById("arrow").style.display="block"; }else{ document.getElementById("arrow").style.display="none"; } }

那时,资产中的所有内容都是'目录将相对于您的根URL提供。所以app.use(express.static(__dirname + '/assets'));是它的正确网址。