简体   繁体   English

使用 post with express:我想从用户那里接收数据,然后用一个新文件将该数据发送回用户

[英]Using post with express: I want to receive data from the user and then send that data back to the user with a new file

I'm creating a node.js webpage of a tabletop card game and I originally built the application on the backend while being able to only handle one game at a time (as I only tested one at a time).我正在创建一个桌面纸牌游戏的 node.js 网页,我最初在后端构建应用程序,同时一次只能处理一个游戏(因为我一次只测试一个)。 Now that I'm incorporating the front end, and need the ability to process multiple games at a time, I'm struggling with how to receive a user's name and id code and either create or join a specific game while still remembering on client side who the player themselves is:现在我正在整合前端,并且需要一次处理多个游戏的能力,我正在努力解决如何接收用户的姓名和 ID 代码以及创建或加入特定游戏的同时仍然记住客户端玩家自己是谁:

Earlier algorithm:之前的算法:

--Have global variable player and update to the user inputed name with post req (via express). -- 拥有全局变量 player 并使用 post req(通过 express)更新用户输入的名称。

--send user gameView.html file --发送用户gameView.html文件

--from there (client side) const socket = io.connect(), socket.emit('newPlayer', socket.id) -- 从那里(客户端) const socket = io.connect(), socket.emit('newPlayer', socket.id)

--create a player object linking the socket id and global variable player --创建一个连接socket id和全局变量player的player对象

--reset the global player variable to "" --将全局播放器变量重置为“”

--continue waiting for the rest of the players --继续等待其他玩家

Obviously this is very sloppy practice and was only being used for the development of the backend.显然,这是非常草率的做法,仅用于后端的开发。 Now that I need to handle multiple users and games my algorithm (ideally) would work as such:现在我需要处理多个用户和游戏,我的算法(理想情况下)可以这样工作:

--User posts their name and an id code via the prePlayView.html file --用户通过 prePlayView.html 文件发布他们的姓名和 ID 代码

--send user the gameView.html file as well as the inputted name and id (this is the part i'm not sure how to implement, as socket id changes after new file is sent so there's no way to track on server side) --向用户发送gameView.html文件以及输入的名称和ID(这是我不确定如何实现的部分,因为在发送新文件后套接字ID会发生变化,因此无法在服务器端进行跟踪)

--On client side, const socket = io.connect(), socket.emit('newPlayer', name, id); -- 在客户端,const socket = io.connect(), socket.emit('newPlayer', name, id);

--if id is blank then create game object with unique id and store in dictionary of games with key id and add player -- 如果 id 为空,则创建具有唯一 id 的游戏对象并存储在具有键 id 的游戏字典中并添加玩家

Any ideas?有任何想法吗? am i fundamentally missing something?我从根本上错过了什么吗? I'm fairly new to node.js.我对 node.js 相当陌生。

Here's my current implementation for reference:这是我目前的实现以供参考:

var app = express();
app.set('port', 5000);
// called when a player enters the game
app.post("/userView.html", function(req, res) {
  var body = "";
  req.on('data', function(char) {
    body += char;
  });

  req.on('end', function () {
    var playerName = body.substr(body.indexOf("=") + 1, body.indexOf("&") - 4);
    var id = body.substr(body.indexOf("&") + 4, body.length);
    body = "";
    var options = {
      headers: {
        'id': id,
        'names': playerName
      }
    }
    res.sendFile(path.join(__dirname, 'userView.html'), options);

  });

I'm going to assume you are using sockets to communicate between your server and client.我将假设您使用套接字在服务器和客户端之间进行通信。

You're right, if you want to handle multiple games at once, you cannot use a global variable on your server to store details of the user currently registering.你是对的,如果你想一次处理多个游戏,你不能在你的服务器上使用全局变量来存储当前注册用户的详细信息。 Using this strategy, only one player can register with the server at a time.使用此策略,一次只有一名玩家可以注册到服务器。

A better strategy would be to remember which clients have connected to the server in the past, and when they make a request, to temporarily load their information just for the duration of their request, and just in the scope of your server's express route handler.更好的策略是记住过去哪些客户端连接到服务器,以及当他们发出请求时,仅在请求期间临时加载他们的信息,并且只是在服务器的快速路由处理程序的范围内。

I'm happy to introduce you to the concept of sessions .我很高兴向您介绍session的概念。 You can use them to remember information about previously connected clients.您可以使用它们来记住有关以前连接的客户端的信息。 There's a middleware that adds sessions to express called express-sessions .有一个中间件可以为 express 添加会话,称为express-sessions Also, assuming you're using sockets.io, there's also an FAQ on using socket.io with express-session .此外,假设您使用的是 sockets.io,还有一个关于将 socket.io 与 express-session 一起使用的常见问题解答。

I cannot cover everything you'll have to change about your architecture here to support multiple games at once, but learning about and utilizing sessions is one possible pathway.我不能涵盖你必须了解你在这里架构,支持同时多场比赛改变了一切,但学习和利用会议是一种可能的途径。 Please take it slow, and bite off one piece at a time.请慢慢来,一次咬掉一块。 Learning how to build web apps to do what you're imagining can be difficult, focusing just on one step at a time is necessary :)学习如何构建网络应用程序来完成您的想象可能很困难,一次只专注于一个步骤是必要的:)

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

相关问题 我想使用ajax post从视图传递codeigniter中的数据,在控制器中处理该数据,然后将结果数组发送回视图 - I want to pass data in codeigniter from view using ajax post, process that data in controller and send back the result array to view 使用Express从Javascript向Node.js发送/接收数据 - Send/receive data from Javascript to Node.js using Express 我想将数据发送到 php 文件进行处理并使用 ajax 接收响应而不刷新页面 - I want to send data to a php file for processing and receive a response using ajax without page refresh 如何从express \\ nodeJS中的发布请求中获取数据,并在提交后将用户重定向到html文件 - how to get data from post request in express\nodeJS and redirect user to html file after submit 我想在用户关闭浏览器选项卡时将数据发布到 API - I want to POST data to an API when user close the browser tab 发送特定于用户组Node Express的数据 - Send data specific to user group Node Express jQuery post 接收数据用快递 - jQuery post receive data with express 我不想用快递发回表单数据,但它确实如此,还是它? - I don't want to send form data back with express, but it does, or does it? 使用 POST 将数据发送回目标 c - Using POST to send data back to objective c 快速更新用户数据 - Express update user data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM