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

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

问题描述

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

来自 app.js

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

在我的 .ejs 中,我已经尝试了这两行

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

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

我的路径看起来像

<预><代码>../app.js./民众/css/style.css

解决方案

在你的 server.js 文件中使用这个

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

并添加css如

不需要/在 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.

From app.js

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

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" />

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

My path looks like

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

解决方案

Use this in your server.js file

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

and add css like

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

dont need / before css like

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

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

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