无法获取CSS文件 [英] Can not get CSS file

查看:103
本文介绍了无法获取CSS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目录设置如下:

app.js
vews
  home.html
  css
    style.css

我的家庭文件是这样的: p>

My home file is like this :

<html>
<head>
  <title></title>
  <link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>

</body>
</html>

我的应用如下所示:

var io   = require('socket.io'),
    url  = require('url'),
    sys  = require('sys'),
    express = require('express'),
    http=require('http');

var app = express();
var server = http.createServer(app);
var socket = io.listen(server);

app.engine('.html', require('ejs').__express);
app.set('views', __dirname + '/views');
app.set('view engine', 'html');

app.get('/', function(req, res){
    res.render('home');
});

app.listen(4000);
sys.puts('server running ' + 'now ' + Date.now());

问题是当我运行应用程序时,无法加载css文件。

The problem is when i run the app, css file can not be loaded.

推荐答案

由于.css文件是静态文件,您必须将它们提供给客户端。但是,您不会将静态文件作为快速中间件提供服务。将以下中间件添加到您的快速应用程序中,然后移动 public 目录下的 css 文件夹(您应该创建一个 public directory

Since .css files are static files you have to serve them to the clients. However, you do not serve static files as a express middleware. Add the following middleware to your express app and move the css folder under the public directory (you should create a public directory)

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

所以你的最终目录结构应该像这样

So your final directory structure should look like this

app.js
views
  home.html
public
  css
    style.css

不要忘记要求路径模块

 var path = require('path')

这篇关于无法获取CSS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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