【问题标题】:node.js express using stylesheets/scriptsnode.js 使用样式表/脚本表达
【发布时间】:2014-08-28 04:56:38
【问题描述】:

我创建了一个网页,开头有以下内容,

<link href="./css/font-awesome.min.css" rel="stylesheet">
<link href="http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600" rel="stylesheet">

<link href="./css/ui-lightness/jquery-ui-1.10.0.custom.min.css" rel="stylesheet">

<link href="./css/base-admin-3.css" rel="stylesheet">
<link href="./css/base-admin-3-responsive.css" rel="stylesheet">
<link rel="stylesheet" href="./compiled/flipclock.css">
<link href="./css/pages/plans.css" rel="stylesheet"> 
<link href="./css/pages/pricing.css" rel="stylesheet"> 
<script src="./js/jquery-1.11.1.js"></script>

<script src="./compiled/flipclock.js"></script>

<link href="./css/custom.css" rel="stylesheet">

我有静态创建的页面,它看起来很完美,但是现在我已经开始将它与 node.js 和 express 集成。从我正在使用的 app.js 加载页面时。 app.get('/liveSale.html', function(req, res){ res.sendfile('liveSale.html'); });

但是,当页面加载时,它不会呈现样式表和脚本。 希望有人能帮忙。

【问题讨论】:

  • 只是想知道,你为什么用res.sendfile,为什么不用res.render()
  • 我只是直接在 socket.io 聊天客户端示例中工作

标签: html css node.js express


【解决方案1】:

看起来我发现了几个重复的线程。

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

这解决了我的问题。

【讨论】:

  • 是的,没错。如果没有设置静态中间件,Express 不会自动从任何地方提供文件。假设您可以设置单独的app.get('/your-js-file.js') 调用来发送文件,但通常express.static() 是要走的路。
猜你喜欢
  • 2021-02-07
  • 2010-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多