Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.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
Javascript nodejs/express包含本地js文件_Javascript_Html_Css_Node.js_Express - Fatal编程技术网

Javascript nodejs/express包含本地js文件

Javascript nodejs/express包含本地js文件,javascript,html,css,node.js,express,Javascript,Html,Css,Node.js,Express,这是我当前的文件夹结构 css app.css js app.js node-modules index.html node-server.js package.json 节点服务器正在托管index.html,但我不知道如何获取要加载的app.js和app.css文件 index.html加载以下内容: <script src="js/app.js"></script> <link rel="stylesheet" type="text/css" hr

这是我当前的文件夹结构

css
   app.css
js
  app.js
node-modules
index.html
node-server.js
package.json
节点服务器正在托管
index.html
,但我不知道如何获取要加载的
app.js
app.css
文件

index.html
加载以下内容:

<script src="js/app.js"></script>
<link rel="stylesheet" type="text/css" href="css/app.css"/>
我知道我需要或加载模块或其他东西,只是不知道是什么

谢谢

如前所述,您需要让Express知道您打算将这些文件托管在一个静态目录中。这在技术上被称为

这应该类似于:

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

// first parameter is the mount point, second is the location in the file system
app.use("/public", express.static(__dirname + "/public"));
这非常简单,我建议您使用某种类型的
公共文件夹,而不是麻烦将特定的文件和文件夹设置为静态

然后从根
index.html
中简单地引用这些文件:

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

希望这对你有帮助

原因 Js本身不提供静态内容,必须定义路由,以便通过Node提供静态内容

解决方案(手动)

var express = require('express'),
    path = require('path'),
    app = express();

app.get('/index.html',function(req,res){
   res.sendFile(path.join(__dirname + '/index.html')); 
});

app.get('/css/app.css',function(req,res){
    res.sendFile(path.join(__dirname + '/css/app.css')); 
});

app.get('/js/app.js',function(req,res){
    res.sendFile(path.join(__dirname + '/js/app.js')); 
});

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

app.listen(8080);
更好的解决方案

var express = require('express'),
    path = require('path'),
    app = express();

app.get('/index.html',function(req,res){
   res.sendFile(path.join(__dirname + '/index.html')); 
});

app.get('/css/app.css',function(req,res){
    res.sendFile(path.join(__dirname + '/css/app.css')); 
});

app.get('/js/app.js',function(req,res){
    res.sendFile(path.join(__dirname + '/js/app.js')); 
});

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

app.listen(8080);
目录结构:

public
 css
   app.css
 js
  app.js
 index.html
代码:


我使用这种语法使它工作

app.use(express.static('public'));
将css和js文件复制到“public”目录下 然后在index.html文件中添加引用

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

//我们在./utils/dbHelper.js中,这里有一些helper函数
函数连接(){
//连接数据库。。。
}
函数closeConnection(){
//关闭与数据库的连接。。。
}
//让我们导出此函数以向外部世界显示它们
module.exports={
connect(),
closeConnection()
};
//现在我们在./main.js中,我们希望使用dbHelper.js中的helper函数
const DbHelper=require('./utils/DbHelper');//导入所有文件并将其命名为DbHelper
DbHelper.connect();//使用点(.)
//或者,我们只能导入所选函数
常数{
连接
紧密连接
}=require('./utils/dbHelper');

connect();//使用不带点的类函数
那么,你的node.js服务器代码在哪里?你必须在你的express应用程序中添加类似于
app.use(express.static('css'))的东西
然后在html中使用
href=“/app.css”
引用它,但最好将
js
css
文件夹放入
public
文件夹,然后将其添加到express app代码中:
app.use(express.static('public')哇,哇!我忘了那个部分了@TomaszKasperek。谢谢。您必须确保将express包括在内:var express=require('express');var-app=express();否则它会告诉你express没有定义,你会想揍婴儿。我觉得这应该是给定的,但为了完整性,我添加了它。抢手货这不是给我的,我引用的例子让我做var-app=require('express')(),我想,这让我很伤心。
<link rel="stylesheet" href="/css/reset.css">