如何使用node,express和ejs包含CSS文件? [英] How can I include css files using node, express, and ejs?

查看:98
本文介绍了如何使用node,express和ejs包含CSS文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照说明 https://stackoverflow.com/a/18633827/2063561 ,但我仍然无法加载styles.css.

I'm trying to follow the instructions to https://stackoverflow.com/a/18633827/2063561, but I still can't get my styles.css to load.

来自app.js

app.use(express.static(path.join(__dirname, 'public')));

在我的.ejs文件中,我尝试了这两行

In my .ejs, I have tried both of these lines

<link rel="stylesheet" type="text/css" href="/css/style.css" />
<link rel="stylesheet" type="text/css" href="/public/css/style.css" />

都不加载css.我进入开发人员的控制台,注意到类型设置为'text/html'而不是'text/css'.

Neither loads the css. I've gone into the developer's console noticed the type is set to 'text/html' instead of 'text/css'.

我的路径看起来像

.
./app.js
./public
    /css
        /style.css

推荐答案

在server.js文件中使用它

Use this in your server.js file

app.use(express.static(__dirname + '/public'));

并添加css之类的

<link rel="stylesheet" type="text/css" href="css/style.css" />

在之类的CSS之前不需要/

dont need / before css like

<link rel="stylesheet" type="text/css" href="/css/style.css" />

这篇关于如何使用node,express和ejs包含CSS文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆