An *_*ame 3 javascript node.js express game-maker
我制作了一个html5游戏(使用GameMaker),它由一个index.html和一个包含游戏依赖关系的文件夹"html5game"构成 - javascript代码和资源.问题是资源数量众多且多样化(声音,精灵等),客户需要它们才能发挥作用.
我正在寻找一种方法来发送它们而不具体命名它们.
我试过了glob模块:
var glob = require( 'glob' );
var files = glob.sync( './html5game/**' ).forEach( function( file ) {
require( path.resolve( file ) );
});
Run Code Online (Sandbox Code Playgroud)
但是,一旦我这样做,我无法找到使用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 !')
})
Run Code Online (Sandbox Code Playgroud)
但它给了我错误:
TypeError: path argument is required to res.sendFile
Run Code Online (Sandbox Code Playgroud)
如果你有其他解决方案,我也有兴趣.谢谢你的回答!
你将无法发送多个这样的文件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)
Run Code Online (Sandbox Code Playgroud)
现在,在您的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+'/');
});
Run Code Online (Sandbox Code Playgroud)
这将index.html
在以下地址上提供您的所有文件(包括):
index.html
)当然,您仍需要确保index.html
正确引用文件中的资产,例如:
<script src="/html5game/xxx.js"></script>
Run Code Online (Sandbox Code Playgroud)
在上面的示例布局的情况下.
index.html
通常会调用包含静态资产(您拥有自己的资产)的顶级目录static
,public
或者html
只要您在调用中使用正确的路径,就可以随意调用它express.static()
.
如果您希望在根路径以外的某个路径中使用游戏,则可以将其指定为app.use
.例如,如果你改变这个:
app.use(express.static(htmlPath));
Run Code Online (Sandbox Code Playgroud)
对此:
app.use('/game', express.static(htmlPath));
Run Code Online (Sandbox Code Playgroud)
然后代替那些网址:
index.html
)这些网址将可用:
index.html
)这里有很多问题与使用Express提供静态文件有关,所以我做了一个工作示例并将其发布在GitHub上,以便人们可以有一个有效的起点并从那里开始:
另请参阅其他一些答案,我将更详细地讨论它:
归档时间: |
|
查看次数: |
6853 次 |
最近记录: |