使用快递将整个文件夹内容发送给客户
Sending whole folder content to client with express
我制作了一个 html5 游戏(使用 GameMaker),它由一个 index.html 和一个包含游戏依赖项的文件夹 "html5game" 组成 - javascript 代码和资源。问题是资源非常多且多样(声音、精灵等),客户端需要它们来播放。
我正在寻找一种无需特别命名即可将它们全部发送的方法。
我尝试了 glob 模块:
var glob = require( 'glob' );
var files = glob.sync( './html5game/**' ).forEach( function( file ) {
require( path.resolve( file ) );
});
但是我想不出使用 res.sendFile() 发送文件的方法。
我试过了
var express = require('express');
var app = express();
[...]
app.get('/aeronavale/jeu', function(req, res){
res.sendFile(__dirname + '/aeronavale/index.html');
res.sendFile(files)
});
[...]
app.listen(3000, function(){
console.log('app started on port 3000, yeah !')
})
但它给了我错误:
TypeError: path argument is required to res.sendFile
如果您有其他解决方案,我也很感兴趣。感谢您的回答!
您将无法像 res.sendFile
那样发送多个文件。您可以在这里做的最直接的事情是:
将您的 index.html
文件和 html5game
目录放入某个公共目录,例如称为 html
并将其放在您的 Node.js 程序所在的位置。目录布局示例为:
/home/you/yourapp:
- app.js (your node program)
- package.json (your package.json etc)
- html (a new directory)
- index.html (your main html to serve)
- html5game (the directory with other files)
- (other files)
现在,在您的 Node 程序中,您可以使用如下内容:
var path = require('path');
var express = require('express');
var app = express();
var htmlPath = path.join(__dirname, 'html');
app.use(express.static(htmlPath));
var server = app.listen(3000, function () {
var host = 'localhost';
var port = server.address().port;
console.log('listening on http://'+host+':'+port+'/');
});
这将在以下地址提供您的所有文件(包括 index.html
):
- http://localhost:3000/(你的
index.html
)
- http://localhost:3000/html5game/xxx.js(您的资产)
当然,您仍然需要确保在 index.html
文件中正确引用您的资产,例如:
<script src="/html5game/xxx.js"></script>
在上面的示例布局的情况下。
包含静态资产的顶级目录(您有 index.html
)通常称为 static
、public
或 html
,但您可以随意命名你喜欢,只要你在调用 express.static()
.
时使用正确的路径
如果你想让你的游戏在根路径以外的其他路径中可用,那么你可以将其指定为 app.use
。例如,如果您更改此:
app.use(express.static(htmlPath));
对此:
app.use('/game', express.static(htmlPath));
然后代替这些 URL:
- http://localhost:3000/(你的
index.html
)
- http://localhost:3000/html5game/xxx.js(您的资产)
这些 URL 将可用:
- http://localhost:3000/game/(你的
index.html
)
- http://localhost:3000/game/html5game/xxx.js(您的资产)
这里的很多问题都与使用 Express 提供静态文件有关,所以我做了一个工作示例并将其发布在 GitHub 上,以便人们可以有一个工作起点并从那里开始:
另请参阅我更详细地讨论它的其他一些答案:
- How to serve an image using nodejs
解决方法是使用 archiver library 压缩目录并在前端解压缩。
我制作了一个 html5 游戏(使用 GameMaker),它由一个 index.html 和一个包含游戏依赖项的文件夹 "html5game" 组成 - javascript 代码和资源。问题是资源非常多且多样(声音、精灵等),客户端需要它们来播放。
我正在寻找一种无需特别命名即可将它们全部发送的方法。
我尝试了 glob 模块:
var glob = require( 'glob' );
var files = glob.sync( './html5game/**' ).forEach( function( file ) {
require( path.resolve( file ) );
});
但是我想不出使用 res.sendFile() 发送文件的方法。
我试过了
var express = require('express');
var app = express();
[...]
app.get('/aeronavale/jeu', function(req, res){
res.sendFile(__dirname + '/aeronavale/index.html');
res.sendFile(files)
});
[...]
app.listen(3000, function(){
console.log('app started on port 3000, yeah !')
})
但它给了我错误:
TypeError: path argument is required to res.sendFile
如果您有其他解决方案,我也很感兴趣。感谢您的回答!
您将无法像 res.sendFile
那样发送多个文件。您可以在这里做的最直接的事情是:
将您的 index.html
文件和 html5game
目录放入某个公共目录,例如称为 html
并将其放在您的 Node.js 程序所在的位置。目录布局示例为:
/home/you/yourapp:
- app.js (your node program)
- package.json (your package.json etc)
- html (a new directory)
- index.html (your main html to serve)
- html5game (the directory with other files)
- (other files)
现在,在您的 Node 程序中,您可以使用如下内容:
var path = require('path');
var express = require('express');
var app = express();
var htmlPath = path.join(__dirname, 'html');
app.use(express.static(htmlPath));
var server = app.listen(3000, function () {
var host = 'localhost';
var port = server.address().port;
console.log('listening on http://'+host+':'+port+'/');
});
这将在以下地址提供您的所有文件(包括 index.html
):
- http://localhost:3000/(你的
index.html
) - http://localhost:3000/html5game/xxx.js(您的资产)
当然,您仍然需要确保在 index.html
文件中正确引用您的资产,例如:
<script src="/html5game/xxx.js"></script>
在上面的示例布局的情况下。
包含静态资产的顶级目录(您有 index.html
)通常称为 static
、public
或 html
,但您可以随意命名你喜欢,只要你在调用 express.static()
.
如果你想让你的游戏在根路径以外的其他路径中可用,那么你可以将其指定为 app.use
。例如,如果您更改此:
app.use(express.static(htmlPath));
对此:
app.use('/game', express.static(htmlPath));
然后代替这些 URL:
- http://localhost:3000/(你的
index.html
) - http://localhost:3000/html5game/xxx.js(您的资产)
这些 URL 将可用:
- http://localhost:3000/game/(你的
index.html
) - http://localhost:3000/game/html5game/xxx.js(您的资产)
这里的很多问题都与使用 Express 提供静态文件有关,所以我做了一个工作示例并将其发布在 GitHub 上,以便人们可以有一个工作起点并从那里开始:
另请参阅我更详细地讨论它的其他一些答案:
- How to serve an image using nodejs
解决方法是使用 archiver library 压缩目录并在前端解压缩。