问题描述
我做了一个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:要求res.sendFile的路径参数
如果您有其他解决方案,我也有兴趣。感谢您的答案!
您将无法使用 res发送多个这样的文件。 SENDFILE
。你可以在这里做的最简单的事情就是这样:
把你的 index.html
code> html5game 目录到一些常见的目录,例如称为 html
,并将其放在您拥有Node.js程序的位置。示例目录布局将是:
/ home / you / yourapp:
- app.js(您的节点程序)
- package.json(你的package.json等)
- html(一个新的目录)
- index.html(你的主要html服务)
- html5game目录与其他文件)
- (其他文件)
现在,在您的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('listen on http://'+ host +':'+ port +'/');
});
这将为您的所有文件(包括 index.html
),地址如下:
- (您的
index.html
) - (您的资产)
当然,您仍然需要确保您正确地在 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:
- (您的
索引。 html
) - (您的资产)
这些网址将可用:
- (您的
index.html
) - (您的资产)
这里有很多问题涉及到使用Express提供静态文件,所以我做了一个工作示例,并将其发布在GitHub上,以便人们可以有一个工作起点,从那里开始: p>
另请参阅一些其他答案,我将在此更详细地介绍:
- 使用
I made an html5 game (using GameMaker), which is constituted of an index.html and a folder "html5game" that contains the dependencies of the game - the javascript code and the resources. The problem is the resources are quite numerous and diverse (sounds, sprites, etc.) and The client needs them all to play.
I am looking for a way to send them all without naming them specifically.
I tried the glob module :
var glob = require( 'glob' );
var files = glob.sync( './html5game/**' ).forEach( function( file ) {
require( path.resolve( file ) );
});
but I can't figure a way to send the files using res.sendFile() once I did that.
I tried
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 !')
})
but it gives me the error :
TypeError: path argument is required to res.sendFile
If you have an other solution, I a also interested. Thanks for your answers !
You will not be able to send multiple file like that with res.sendFile
. The most straightforward thing that you can do here would be this:
Put your index.html
file and your html5game
directory into some common directory, e.g. called html
and put it where you have your Node.js program. An example directory layout would be:
/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)
Now, in your Node program you can use something like this:
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+'/');
});
This will serve all of your files (including index.html
) on addresses like:
- http://localhost:3000/ (your
index.html
) - http://localhost:3000/html5game/xxx.js (your assets)
Of course you still need to make sure that you refer to your assets in your index.html
file correctly, for example with:
<script src="/html5game/xxx.js"></script>
in the case of the example layout above.
The top level directory with your static assets (where you have your index.html
) is usually called static
, public
or html
but you can call it whatever you like, as long as you use the correct path in your call to express.static()
.
If you want to have your game available in some path other than the root path then you can specify it to app.use
. For example if you change this:
app.use(express.static(htmlPath));
to this:
app.use('/game', express.static(htmlPath));
Then instead of those URLs:
- http://localhost:3000/ (your
index.html
) - http://localhost:3000/html5game/xxx.js (your assets)
those URLs will be available instead:
- http://localhost:3000/game/ (your
index.html
) - http://localhost:3000/game/html5game/xxx.js (your assets)
A lot of questions here are related to serving static files with Express so I made a working example and posted it on GitHub so that people could have a working starting point and go from there:
See also some other answers where I talk about it in more detail:
- How to serve an image using nodejs
- Failed to load resource from same directory when redirecting Javascript
- onload js call not working with node
- Loading partials fails on the server JS
- Node JS not serving the static image
这篇关于发送整个文件夹内容到客户端快递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!