Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 查找css文件节点js_Node.js_File_Express - Fatal编程技术网

Node.js 查找css文件节点js

Node.js 查找css文件节点js,node.js,file,express,Node.js,File,Express,嗨,我是nodejs新手,刚刚成功在线部署了nodejs应用程序。现在我想知道如何链接CSS、JS或图像文件,因为当我像以前那样尝试链接时,会出现错误get(notfound) 文件夹体系结构是: --public --assets --css index.css --js --views --index.ejs --node modules app.js package.json 假设我想在index.ejs中对链接index.css进行编码,那

嗨,我是nodejs新手,刚刚成功在线部署了nodejs应用程序。现在我想知道如何链接CSS、JS或图像文件,因为当我像以前那样尝试链接时,会出现错误
get(notfound)

文件夹体系结构是:

--public
    --assets
    --css
       index.css
    --js
--views
    --index.ejs
--node modules
app.js
package.json
假设我想在
index.ejs
中对链接
index.css
进行编码,那么我需要在我的
app.js
文件中写些什么呢

app.js代码:

var express = require('express');
var app = express.createServer();

app.set('view engine', 'ejs');

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

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


app.listen(8080,'IP_ADRESS');
index.ejs代码:

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

在express中提供静态文件的基本设置:

var express = require("express");
var app = express();
...
app.use(express.static(__dirname + '/public'));
...
app.listen(3000);
现在在.ejs中加载css样式或js脚本:

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

首先,有一种东西叫做静态文件。基本上,这是一个通过互联网发送的文件,没有任何修改。图像和CSS文件通常是静态文件

看起来您已将静态文件放在名为
public
的文件夹中

Express具有用于发送静态文件的内置功能,称为
Express.static
。您可以这样使用它:

// Require the modules we need.
var express = require('express');
var path = require('path');

// Create an Express app.
var app = express();

// Get the path to the `public` folder.
// __dirname is the folder that `app.js` is in.
var publicPath = path.resolve(__dirname, 'public');

// Serve this path with the Express static file middleware.
app.use(express.static(publicPath));

// ...
您现在应该能够看到静态文件。如果您的站点通常可在
http://localhost:3000
,您将能够在
http://localhost:3000/css/index.css


如果你想知道太多关于express.static的信息,可以深入查看express的静态文件。

你应该在html文件中添加css,并将express.static文件夹设置为“public”。向我们展示您的app.js,以便我们可以帮助您。是否更清楚?这对于Express 2.x是正确的。这在Express 3+中发生了变化。我正在使用Express 3+。当我检查google chrome上的源代码时,你的代码似乎有点不对劲。我可以看到index.css文件,当我编辑它时,它会编辑我的代码,但当我尝试编辑位于我服务器上的index.css文件时,没有更新。感谢您的链接,它意识到您的浏览器可能正在缓存
index.css
。尝试在浏览器中执行此操作。在Express应用程序的其他位置是否有缓存中间件?发布你的
app.js
(或同等版本)会很有用。啊,对不起,刚才看到你更新了你的问题。你确定你正在更新文件系统上的
index.css
吗?它确实不起作用,但我在我的应用程序中发现了一些错误,我想我没有很好地使用express,所以我从一开始就重新启动了,并尝试使用express generator。