节点js服务器:我想在同一个响应中响应同时包含HTML和JSON的浏览器请求,这可能吗?

时间:2022-12-29 16:56:04

Basically, my thought process is as follows.

基本上,我的思考过程如下。

Client/browser requests html file login.html. This file contains a form with a username and password field. The submission button sends a POST request to the server with the given user and password field values. Simple, so far.

客户端/浏览器请求html文件login.html。此文件包含带有用户名和密码字段的表单。提交按钮使用给定的用户和密码字段值向服务器发送POST请求。很简单,到目前为止。

The server then checks a folder /users of JSON files of the naming convention username.json. For instance, if the client submits the username "John", it checks to see whether or not the file John.json exists. If it does, it compares the password value submitted in the POST request to that of the JSON file. If that matches, it returns the login-success html file, otherwise redirects back home.

然后,服务器检查命名约定username.json的JSON文件的文件夹/用户。例如,如果客户端提交用户名“John”,它将检查文件John.json是否存在。如果是,则将POST请求中提交的密码值与JSON文件的密码值进行比较。如果匹配,则返回login-success html文件,否则重定向回home。

That's where my problem lies. If it succeeds, I also wan't it to 'respond' with the JSON file matched to it, but the request seemingy only allows for the response to be one content-type, or to be read from a single file.

这就是我的问题所在。如果成功,我也不想用它匹配的JSON文件“响应”,但看似只允许响应是一个内容类型,或者从单个文件中读取。

So, I'm just not sure how the client will know which JSON file to read based on the given response. I'm sure there's an easy solution and that I'm being an idiot, but any help would be appreciated.

所以,我只是不确定客户端如何根据给定的响应知道要读取哪个JSON文件。我确信有一个简单的解决方案,我是一个白痴,但任何帮助将不胜感激。

Thanks in advance!

提前致谢!

2 个解决方案

#1


1  

You can respond an HTML file and when document is load, respond the json file,
for example if you use jquery as your client side framework:

您可以响应HTML文件,当文档加载时,响应json文件,例如,如果您使用jquery作为客户端框架:

$(document).ready(function(){
    $.get(URL_To_JsonFile,Params,function(data){
        //do somthing with json file
    })
})

#2


0  

In the end, I decided to use the Sockets.io module in order to pass the client my json data. This enabled me to open up a TCP connection on the back of my HTTP server between the server and the client. When the password submitted matches the password in the json file, the server emits a "login-success" event to that client and with that event passes the json file used in the server. The client side script accepts that response and parses that data for use. Here's a sample:

最后,我决定使用Sockets.io模块来传递客户端我的json数据。这使我能够在服务器和客户端之间的HTTP服务器背面打开TCP连接。当提交的密码与json文件中的密码匹配时,服务器向该客户端发出“login-success”事件,并且该事件传递服务器中使用的json文件。客户端脚本接受该响应并解析该数据以供使用。这是一个示例:

server.js - Server side script

server.js - 服务器端脚本

if (password == json.password) {
    io.on("connection", function(socket){
        socket.emit("login-success", json);
    }
}

app.js - Client side script

app.js - 客户端脚本

var socket = io("http://localhost:8888");
var username;
socket.on("login-success", function(json){
    username = JSON.parse(json).username;
}

This also means the data only has to be read in once, the object it's assigned to is just passed to the client via the TCP stream established between it and the server. Hoorah!

这也意味着数据只需要读入一次,它所分配的对象只是通过它与服务器之间建立的TCP流传递给客户端。 Hoorah!

Thanks very much for the responses everyone.

非常感谢大家的回复。

#1


1  

You can respond an HTML file and when document is load, respond the json file,
for example if you use jquery as your client side framework:

您可以响应HTML文件,当文档加载时,响应json文件,例如,如果您使用jquery作为客户端框架:

$(document).ready(function(){
    $.get(URL_To_JsonFile,Params,function(data){
        //do somthing with json file
    })
})

#2


0  

In the end, I decided to use the Sockets.io module in order to pass the client my json data. This enabled me to open up a TCP connection on the back of my HTTP server between the server and the client. When the password submitted matches the password in the json file, the server emits a "login-success" event to that client and with that event passes the json file used in the server. The client side script accepts that response and parses that data for use. Here's a sample:

最后,我决定使用Sockets.io模块来传递客户端我的json数据。这使我能够在服务器和客户端之间的HTTP服务器背面打开TCP连接。当提交的密码与json文件中的密码匹配时,服务器向该客户端发出“login-success”事件,并且该事件传递服务器中使用的json文件。客户端脚本接受该响应并解析该数据以供使用。这是一个示例:

server.js - Server side script

server.js - 服务器端脚本

if (password == json.password) {
    io.on("connection", function(socket){
        socket.emit("login-success", json);
    }
}

app.js - Client side script

app.js - 客户端脚本

var socket = io("http://localhost:8888");
var username;
socket.on("login-success", function(json){
    username = JSON.parse(json).username;
}

This also means the data only has to be read in once, the object it's assigned to is just passed to the client via the TCP stream established between it and the server. Hoorah!

这也意味着数据只需要读入一次,它所分配的对象只是通过它与服务器之间建立的TCP流传递给客户端。 Hoorah!

Thanks very much for the responses everyone.

非常感谢大家的回复。