如何映射到URL路径的Node.js [英] How to map URL to node.js route

查看:272
本文介绍了如何映射到URL路径的Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的UI路由器角度和Node.js的作为API调用到另一台服务器我的UI服务器。现在,我的浏览器的URL(基于下拉选择动态)不映射到服务器。

例如,浏览器URL为/家庭的color = red&放大器;?&安培;大小=大当我将用户输入到节点。当我复制并粘贴到另一个浏览器窗口的URL,我要的颜色和尺寸的下拉列表中已被选中为红色和大,从API调用结果的基础上所显示的选择。我怎样才能做到这一点?

我AngularJS控制器code:

  $ scope.getResults =功能(){    $ location.search('色',$ scope.myColor);
    $ location.search('大小',$ scope.mySize);    server.getResults($ scope.myColor,$ scope.mySize)
    .success(功能(数据){
        结果=数据;
    });
};

AngularJS用于上述功能的服务:

  app.factory('服务器',['$ HTTP',函数($ HTTP){
    返回{
        getResults:功能(颜色,大小){
            VAR REQ = {};
            req.color =颜色;
            req.size =大小;            返回$ HTTP({
                方法:GET,
                网址:'结果',
                params:一个REQ
            });
        }
    }
}]);

UI路由器在角:

  $ stateProvider.state('家',{
    网址:'/家,
    templateUrl:/home.html',
    控制器:'MainCtrl',
    reloadOnSearch:假的
})

在Node.js的,我有我的路线是这样的:

  app.get(/结果功能(REQ,RES){    VAR API ='一些API调用/+ req.query.color +/+ req.query.size;    请求(API,功能(错误响应,API){        如果(误差放大器和;!&安培; response.status code == 200){
            res.json({
                回应:API
            });
        }
    });
});


解决方案

在您的code你写的查询参数,但你需要阅读它们,试试这个:

  $ scope.getResults =功能(){  $ scope.myColor = $ location.search()的颜色。
  $ scope.mySize = $ location.search()的大小。  server.getResults($ scope.myColor,$ scope.mySize)
  .success(功能(数据){
    结果=数据;
  });
};

I am using ui-router with Angular and Node.js as my UI server for API calls to another server. Right now, my browser URL (dynamic based on dropdown selections) does not map to the server.

For example, the browser URL is "/home?color=Red&&size=Large" when I send the user inputs to Node. When I copy and paste that URL in another browser window, I want the color and size dropdowns to already be selected as Red and Large, and results from API call based on the selections displayed. How can I accomplish this?

My AngularJS controller code:

$scope.getResults = function() {

    $location.search('color', $scope.myColor);
    $location.search('size', $scope.mySize);

    server.getResults($scope.myColor, $scope.mySize)
    .success(function(data) {
        results = data;
    });
};

AngularJS service for the above function:

app.factory('server', ['$http', function($http){
    return { 
        getResults : function(color, size) {
            var req = {};
            req.color = color;
            req.size = size;

            return $http({
                method: 'GET', 
                url: 'results',
                params : req
            });
        }
    }
}]);

ui-router in Angular:

$stateProvider.state('home', {
    url: '/home',
    templateUrl: '/home.html',
    controller: 'MainCtrl',
    reloadOnSearch: false
})

In Node.js, I have my route like this:

app.get("/results", function (req, res) {

    var api = 'some api call/' + req.query.color + '/' + req.query.size;

    request(api, function (error, response, api) {

        if (!error && response.statusCode == 200) {
            res.json({
                Response: api
            });
        }
    });
});

解决方案

In your code you wrote query parameters but you need to read them, try this:

$scope.getResults = function() {

  $scope.myColor = $location.search().color;
  $scope.mySize = $location.search().size;

  server.getResults($scope.myColor, $scope.mySize)
  .success(function(data) {
    results = data;
  });
};

这篇关于如何映射到URL路径的Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆