Fir*_*any 4 javascript json node.js express
What specific changes need to be made to the code below so that the JSON contents of the message variable can be sent from the Node.js server to the AngularJS client?
The following code is in the routing file of a Node.js app. The code below generates JSON and then redirects control to the redirectURL='/' handler, which sends the ./public/index.html file to the AngularJS client.
message在此事务中将 JSON发送到 AngularJS 客户端很重要,以便 AngularJS 浏览器应用程序可以负责保留用户的唯一身份。这样,来自 AngularJS 浏览器应用程序的后续请求可以由该 Node.js 应用程序的数百个克隆中的任何一个提供服务,每个克隆将使用它在每个请求中收到的 JSON 连接到 Redis 集群,以管理多个用户的身份。要求。这将使 Node.js 不必记住用户身份。
app.get('/some_endpoint', function(req, res) {
var redirectURL = '/';
request.post(otherServerUrl, function(err, response, body){
if(err){console.log('ERROR with token request.')}
var data = JSON.parse(body);
getUser(data.access_token).then(function(message) {
console.log('in callback, jwtJSON is: ');console.log(message);
res.redirect(redirectURL);
});
});
});
app.get('*', function(req, res) {
res.sendfile('./public/index.html'); // load the single view file (angular will handle the front-end)
});
Run Code Online (Sandbox Code Playgroud)
请注意,app.get('*', function(req, res)还需要能够处理来自未重定向的用户的请求app.get('/some_endpoint', function(req, res),因此message服务器上没有任何要发送到浏览器应用程序的 JSON。
如果您想要做的是将信息传递到将从重定向加载的页面,那么通常的方法是将信息编码为重定向 URL 上的查询参数。
这将通过重定向传递到新加载的页面,然后该页面可以解析查询参数以获取信息。
也可以在重定向之前将信息放入 cookie 并让重定向页面从 cookie 中获取信息,但如果您在重定向过程中使用不同的域,cookie 可能会很复杂或不切实际(因为 cookie 与特定域)。
您不能只返回带有重定向响应的 JSON,因为重定向不会将 JSON 传递到重定向页面 - 这不是重定向的工作方式。重定向只是加载与新 URL 对应的页面。
| 归档时间: |
|
| 查看次数: |
5650 次 |
| 最近记录: |