Angular和Express路由如何在一个mean.js应用程序中一起工作? [英] How does Angular and Express routing work together in a mean.js app?

查看:141
本文介绍了Angular和Express路由如何在一个mean.js应用程序中一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力争取Angular和Express Routing(顺便说一下,我对Express很新),我一直在使用Angular - 与ui-router一起处理路由,但现在我开始构建一个MEAN.js应用程序我注意到我可以处理服务器端路由和客户端路由...这就是让我困惑,这里有一些我的问题:



<
  • 如果我切换到Express路由,我还会有一个SPA?

  • 可以我同时使用两个?怎么样?这是好习惯吗?是否有任何好处?

  • 我什么时候只使用其中一个?

  • 我将如何处理路线参数?

  • 等...



  • 如果有人可以详细解释这些问题,还有其他额外的东西,人们需要知道我将完全感谢。



    另外,我想知道的是:我只需要在Express中设置服务器的东西,还是需要在Node中编写代码?

    解决方案

    Express中的路由与角度有明显差异



    in express


    路由是指对
    应用程序的端点(URI)的定义以及它如何响应客户端请求。


    所以如果你使用angularjs做路由,那么你不需要使用express设置你的html模板的路由。



    您只需使用express即可创建您的RESTAPI,然后使用这些A PI使用angularjs $ http $ resource



    摘要:




    • 在Angular路由支持SPA背后的想法。最终,您希望通过Angular处理基于UI的路由更改(即不需要服务器调用/逻辑)。任何路由更改都会影响后端,最终需要服务器逻辑,应该使用Express路由。



    示例



    这是创建休息API的快速路由。

      app =表现(); 

    app.get('/ dowork',function(res,req){
    console.log(req.params.msg);
    / ...代码你的工作... /
    });

    现在在angularjs调用做工作

      $ http.get('http:// localhost:8080 / dowork',{msg:hi})。success(function(data){
    console。 log(data);
    });


    I'm struggling about Angular and Express Routing (by the way, i'm somehow new to Express), i've been handling routes with Angular — with ui-router — but now that i'm starting to build a MEAN.js Application i notice that i can handle server-side routing and client-side routing... and that's what makes me get confused, here are some of my questions:

    • How are they different?
    • If i switch to Express routing will i still have a SPA?
    • Can i use both at same time? How? Is it good practice? Does it has any benefit?
    • When should i use only one of them?
    • How will i handle route parameters?
    • etc...

    If someone can explain with detail these questions, and other extra things people need to know i'll be totally thankful.

    Also, something that i want to know is: Do i only have to setup server things in Express or do i need to code in Node?

    解决方案

    Routing in Express is sightly different then what it is in angular

    In express

    Routing refers to the definition of end points (URIs) to an application and how it responds to client requests.

    So if you are doing the routing using angularjs then you do not need to set the route for your html template using express.

    you simply use express in a way to create your RESTAPI and then consume those API using angularjs $http or $resource

    Summary:

    • Routing in Angular supports the idea behind a SPA. Ultimately you want to handle UI based route changes (i.e. no server call/logic needed) via Angular. Any route change that hits the backend, and ultimately requires server logic, should use Express routing.

    Example

    This is express routing to create rest API.

    app = express();
    
    app.get('/dowork',function(res,req){
        console.log(req.params.msg);
      /... code to do your work .../
    });
    

    now in angularjs call do work

    $http.get('http://localhost:8080/dowork',{"msg":"hi"}).success(function(data){
     console.log(data);
    });
    

    这篇关于Angular和Express路由如何在一个mean.js应用程序中一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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