将数据传递给客户端JavaScript(最好不带查看引擎或cookie)

第一部分:问题介绍

小故事:我需要将数据从服务器传递到客户端,但这些数据不需要显示在客户端。 相反,客户端套接字使用数据join正确的房间。 转到第二部分:解决scheme的尝试

漫长的故事:

对于我的第一个Web开发项目,我正在尝试创build一个远程演示者(即使用移动设备来控制桌面上的Google幻灯片演示文稿)。

在前端,我一直使用普通的HTMl,CSS和vanilla JS,而后端则涉及NodeJS,Express框架和Socket.io。

以下是用户stream程的简要概述:当用户通过桌面上的Googlelogin进行身份validation时,网站将打开一个套接字连接,该连接自动连接到由套接字ID标识的一个房间。 用户在桌面上login后,用户将(现在)将此套接字标识视为可用于在移动端login的“密钥”。

因此,我有一个server.js处理路由和表单操作(即移动端提交的密钥),如下所示:

 // Store all the currently connected web clients var webClients = []; // Index route -- home page for website and mobile site app.get('/', function (req, res) { // Basic user agent check - test for mobiles var userAgent = req.headers['user-agent']; if (/mobile/i.test(userAgent)) { // Send mobile to the mobile login site res.sendFile(__dirname + '/pages/mobile.html'); } else { // Send desktop to the main site res.sendFile(__dirname + '/pages/index.html'); } }); // Dealing with secret key input app.post('/secretKey', function(req, res) { // Store the secret key in a variable first var secretKey = req.body.secretKey; // Check if the secret key matches with any key in the database (current session) var index = webClients.indexOf(secretKey); // Send the user to the mobile controls if there is something that matches if (index != -1) { res.sendFile(__dirname + '/pages/mobileControl.html'); } else { res.redirect('/'); } } }); 

不幸的是,我遇到了麻烦。 移动控件页面加载完成后,另一个套接字实例将在移动端打开。 为了确保移动控制正确的演示文稿,我需要让移动端上的套接字与桌面套接字(即桌面套接字标识的房间)连接到同一个房间。 因此,我需要将桌面套接字Id传递给移动端,以便移动套接字可以连接到正确的房间。

第二部分:解决scheme尝试

我知道其他几个用户已经提出了类似的问题。 例如:

  • 在Node Express中的res.sendfile中传递数据
  • res.sendFile发送静态文件+对象
  • 基本的Ajax发送/接收与node.js

从这些,我可以总结几个重要的build议:

  1. 使用如Jade,ejs等模板系统

我看着Jade和ejs,而且我不愿意使用这些模板系统,因为我怀疑它们可能是我想在这里实现的东西的矫枉过正 – 只需将string从服务器传递到客户端JavaScript而不呈现string视图。 此外,我并不需要这些模板系统提供的部分,进一步加强了我的信念,这可能是过度的。

  1. 将数据存储在cookie中,并在客户端JavaScript上访问cookie

目前,这个解决scheme最吸引我的只是因为它似乎是简单的出路。 我可以简单地做一些事情:

 var options = { headers: { 'set-cookie' : roomId=secretKey } }; res.sendFile(__dirname + '/pages/mobileControl.html', options); 

然后,我可以通过像document.cookie这样的方法访问mobileControl.js中的cookie中的数据。 但是,我已经读过,这种方法存在安全问题,例如跨站点脚本(XSS)攻击。 因此,我也不愿意使用这种方法。

  1. 将数据发送到另一个路由,并在呈现html之后使用XMLHttpRequest来检索数据

除了这三种方法之外,我还研究了实现用户会话,但我不认为这是我的解决scheme,因为用户会话可能会在稍后阶段进入,以允许login持续。

那么,你认为我应该在这种情况下使用哪种方法? 我应该跟2一起去调整cookie的HttpOnly字段吗? 任何其他build议? 非常感谢!

如果你不想使用一个完整的模板引擎(尽pipe我认为这不会超过顶端 – 这就是模板引擎的目的),那么如何做一个单一的stringreplace?

把一些像

 <script>var SECRET_KEY = '{{{SECRET_KEY}}}';</script> 

在你的HTML文件中,传输文件而不是发送它,并使用像stream-replace这样的东西来replace你的密钥{{{SECRET_KEY}}}

未经testing的例子:

 var fs = require('fs'); var replace = require('stream-replace'); app.post('/secretKey', function(req, res) { // Store the secret key in a variable first var secretKey = req.body.secretKey; // Check if the secret key matches with any key in the database (current session) var index = webClients.indexOf(secretKey); // Send the user to the mobile controls if there is something that matches if (index != -1) { fs.createReadStream(__dirname + '/pages/mobileControl.html') .pipe(replace('{{{SECRET_KEY}}}', secretKey)) .pipe(res); } else { res.redirect('/'); } }); 

然后你就可以在脚本中使用SECRET_KEYvariables。