了解从客户端(javascript)到服务器端(node.js)的路由

时间:2022-06-20 16:00:03

I'm new at web apps and at the moment I'm trying to wrap my head around routing from client side to server side and back. I ran in to a problem where I was doing xmlhttprequest on my client side to get a json, which was working. But now that im not running locally none of the GETS are working. So I figured I have to do routing to server side, do the request() to get the json, which I can.

我是网络应用程序的新手,目前我正试图将客户端和服务器端之间的路由包围起来。我遇到了一个问题,我在客户端执行xmlhttprequest以获取一个正在运行的json。但是现在我没有在本地运行,没有一个GETS正在运作。所以我想我必须做到路由到服务器端,做request()来获取json,我可以。

But now what I don't understand is how to pass that json back to client side to use the function there, since all my functions that use this json are there. Is this possible? or do I have to do everything server side now?

但是现在我不明白的是如何将json传递回客户端以使用那里的函数,因为我使用这个json的所有函数都在那里。这可能吗?或者我现在必须做服务器端的一切吗?

server side

server.get('thankyou.html/something', function(req, res) {

    var options = {
        url: 'https://**.***.**.**:****/****/*******/',
        rejectUnauthorized: false,   
        method: 'GET'
    };

    request(options, function (error, response, body) {
        if (error) console.log(error);
        else displaytable(body);//<------- clientside funtion
    });
});

client side

var uri = 'thankyou.html/something';

function addTable() {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && (xhr.status == 201 || xhr.status == 200)) {
           // var json = JSON.parse(xhr.responseText);
            displaytable(json);
        }

    };
    xhr.open("GET", uri, true);
    xhr.send();
}

I think I'm not doing the routing right either.

我想我也没有做正确的路由。

1 个解决方案

#1


1  

What's the expected flow here? client -> your server @ thankyou.html/something -> some other server url (the one you have censored) -> response back to your server -> response back to client -> client uses response to display table?

这里的预期流量是多少?客户端 - >你的服务器@ thankyou.html / something - >一些其他服务器url(你已审查的那个) - >响应你的服务器 - >响应回客户端 - >客户端使用响应显示表?

Either way, you definitely can't call client functions from your server. Not like that, anyways. You'll need to return the body with something like res.json(body) (what routing / server library are you using?), and then parse the xhr.responseText, like your commented-out line was doing. Then you'll have the json on the client, and can continue as expected.

无论哪种方式,您绝对无法从服务器调用客户端功能。反正不是那样的。你需要用res.json(body)之类的东西返回正文(你正在使用什么路由/服务器库?),然后解析xhr.responseText,就像你注释掉的行一样。然后你将在客户端上拥有json,并且可以按预期继续。

Make sure if your request call returns an error that you pass the error through to the client as well, or it will hang until timeout.

确保您的请求调用返回错误,您将错误传递给客户端,否则它将挂起直到超时。

#1


1  

What's the expected flow here? client -> your server @ thankyou.html/something -> some other server url (the one you have censored) -> response back to your server -> response back to client -> client uses response to display table?

这里的预期流量是多少?客户端 - >你的服务器@ thankyou.html / something - >一些其他服务器url(你已审查的那个) - >响应你的服务器 - >响应回客户端 - >客户端使用响应显示表?

Either way, you definitely can't call client functions from your server. Not like that, anyways. You'll need to return the body with something like res.json(body) (what routing / server library are you using?), and then parse the xhr.responseText, like your commented-out line was doing. Then you'll have the json on the client, and can continue as expected.

无论哪种方式,您绝对无法从服务器调用客户端功能。反正不是那样的。你需要用res.json(body)之类的东西返回正文(你正在使用什么路由/服务器库?),然后解析xhr.responseText,就像你注释掉的行一样。然后你将在客户端上拥有json,并且可以按预期继续。

Make sure if your request call returns an error that you pass the error through to the client as well, or it will hang until timeout.

确保您的请求调用返回错误,您将错误传递给客户端,否则它将挂起直到超时。