使用 express 将整个文件夹内容发送给客户端 [英] Sending whole folder content to client with express

查看:18
本文介绍了使用 express 将整个文件夹内容发送给客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个 html5 游戏(使用 GameMaker),它由一个 index.html 和一个文件夹html5game"组成,其中包含游戏的依赖项 - javascript 代码和资源.问题是资源非常多且多种多样(声音、精灵等),并且客户端需要它们全部播放.

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.

我尝试了 glob 模块:

I tried the glob module :

var glob = require( 'glob' );    

var files = glob.sync( './html5game/**' ).forEach( function( file ) {
      require( path.resolve( file ) );
});

但是一旦我这样做了,我就无法找到使用 res.sendFile() 发送文件的方法.

but I can't figure a way to send the files using res.sendFile() once I did that.

我试过了

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

如果您有其他解决方案,我也有兴趣.感谢您的回答!

If you have an other solution, I a also interested. Thanks for your answers !

推荐答案

您将无法使用 res.sendFile 发送多个文件.您可以在这里做的最直接的事情是:

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:

将您的 index.html 文件和您的 html5game 目录放入某个公共目录中,例如调用 html 并将其放在您的 Node.js 程序中.一个示例目录布局是:

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)

现在,在您的 Node 程序中,您可以使用以下内容:

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+'/');
});

这将在以下地址提供您的所有文件(包括 index.html):

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)

当然,您仍然需要确保在 index.html 文件中正确引用您的资产,例如:

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.

包含静态资产的顶级目录(您在其中拥有 index.html)通常称为 staticpublichtml 但你可以随意调用它,只要你在调用 express.static() 时使用正确的路径.

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().

如果您想让您的游戏在除根路径之外的某个路径中可用,那么您可以将其指定为 app.use.例如,如果您更改此内容:

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));

为此:

app.use('/game', express.static(htmlPath));

然后代替那些网址:

  • http://localhost:3000/ (your index.html)
  • http://localhost:3000/html5game/xxx.js (your assets)

这些网址将可用:

  • http://localhost:3000/game/ (your index.html)
  • http://localhost:3000/game/html5game/xxx.js (your assets)

这里的很多问题都与使用 Express 提供静态文件有关,所以我制作了一个工作示例并将其发布在 GitHub 上,以便人们可以有一个工作起点并从那里开始:

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:

这篇关于使用 express 将整个文件夹内容发送给客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆