简体   繁体   English

Node.js Express:将URL和会话中的数据传递到提供的JavaScript文件中

[英]Node.js Express: Passing data from URL and session into served JavaScript file

I've been building a web-socket application in which the client opens a link to a game instance, and then the server attempts to connect the client to the respective Socket.io room on which the game will transmit information. 我一直在构建一个Web套接字应用程序,客户端打开游戏实例的链接,然后服务器尝试将客户端连接到游戏将传输信息的相应Socket.io房间。 For example, connecting to '/game/abc' would load up the game page and connect the socket on the page to the 'abc' room on the server. 例如,连接到'/ game / abc'会加载游戏页面并将页面上的套接字连接到服务器上的'abc'房间。

The problem with this is getting the client JavaScript file to emit the game ID and the username of the user connecting. 这样做的问题是让客户端JavaScript文件发出游戏ID和用户连接的用户名。 I want it to act in the following way: 我希望它以下列方式行事:

Client.js Client.js

var socket = io();
socket.emit("newUser", username, gameID); 

I have managed to accomplish this by passing both my client.html and client.js page through an Express template renderer: 我设法通过Express.html模板渲染器传递client.html和client.js页面来实现此目的:

Server.js Server.js

app.get(/^\/game\/([a-zA-Z0-9]*)$/, function(req, res){
    var game = req.params[0];
    var user = req.session.name; //gets username stored in session
    res.render("client.html", {username: user, gameName: game});
});

app.get(/game\/(.*)\/client.js/, function(req,res){
    res.render("client.js", {username: req.session.name, gameName: req.params[0]});
});

The second app.get() allows for the gameName to be passed along to client.js through client.html in the form of a parameter in the url. 第二个app.get()允许gameName以url中的参数形式通过client.html传递给client.js。

Client.html Client.html

 <script src="{{gameName}}/client.js"></script>

Finally after two passes, the game ID and username both are put into client.js by the template engine. 最后两次传递后,游戏ID和用户名都由模板引擎放入client.js。

Client.js Client.js

var socket = io();
socket.emit("newUser", "{{username}}", "{{gameName}}");
//leads to socket.emit("newUser", "user", "abc"); when passed through renderer

Although this gets the job done, it feels incredibly convoluted and indirect. 虽然这可以完成工作,但感觉令人难以置信的复杂和间接。 I've looked up alternatives to this, with the answer at node.js express rendering inside included js files recommending to use AJAX calls. 我已经找到了替代方案,在node.js表达内部包含js文件,建议使用AJAX调用。 However, I have not been able to figure out how to exactly configure such an AJAX call. 但是,我还没有弄清楚如何准确配置这样的AJAX调用。 Is there a more effective way to overcome this problem? 有没有更有效的方法来克服这个问题?

You can simplify all of this and avoid rendering the templates in Express to pass that variable in this case. 您可以简化所有这些操作,并避免在Express中渲染模板以传递该变量。

You already have the gave name available to your client-side code in the window.location object. 您已经在window.location对象中为您的客户端代码提供了给出的名称。 You can either parse it manually with a simple regex (in this case) or you can use something that is called a client-side router which there are a lot to choose from. 您可以使用简单的正则表达式手动解析它(在这种情况下),或者您可以使用一些称为客户端路由器的东西 ,有很多可供选择。

There is one simple client-side router inspired by Express.js: Page.js , which would allow you to use a very similar code that you use right now in Express. 有一个受Express.js启发的简单客户端路由器: Page.js ,它允许您使用Express中现在使用的非常相似的代码。

Many client-side frameworks have routers build in. 许多客户端框架都内置了路由器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM