Angular和Express路由如何在一个mean.js应用程序中一起工作? [英] How does Angular and Express routing work together in a mean.js app?
问题描述
<
如果有人可以详细解释这些问题,还有其他额外的东西,人们需要知道我将完全感谢。
另外,我想知道的是:我只需要在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屋!