[英]Angular JS - Express Routing
I am running a MEAN framework with express routing requests. 我正在运行带有明确路由请求的MEAN框架。 I have two main routes to
public/
and app
. 我有两条通往
public/
和app
主要路线。
With the APP being an API and public being a set of web pages which reads data from the API. APP是一个API,而public是一组从API读取数据的网页。
// Setting the app router and static folder
app.use(express.static(path.resolve('./public')));
I have two controllers in the public folder, home and header. 我在公用文件夹中有两个控制器,即home和header。
In the home controller I am using Angular JS to call the API and return the results. 在家庭控制器中,我使用Angular JS调用API并返回结果。
The API allows for filtering through the use of query strings: 该API允许通过使用查询字符串进行过滤:
$http.get('http://search?sumBoth=1&customer=' + customer)
I would like to build up a route specific to this controller along the lines of 我想沿着
http://url/customers/CustomerName
Where CustomerName
would be set as the customer variable 将
CustomerName
设置为客户变量的位置
Question 题
a) Is this best done in Angular or Express? a)最好在Angular或Express中完成吗? b) Where and how do I implement such routing?
b)我在哪里以及如何实现这种路由?
I hope this question is well received, please comment , if you need more information. 我希望这个问题能得到很好的答复 ,如果您需要更多信息,请发表评论 。
I understand that the response of $http.get('http://host/path?sumBoth=1&customer=' + customer)
is a list of search results. 我了解
$http.get('http://host/path?sumBoth=1&customer=' + customer)
的响应是搜索结果的列表 。 In that case the path should be a collection path, it's not really a best practice to have search terms in the path. 在这种情况下,该路径应为集合路径,在路径中包含搜索字词并不是最佳做法。 Subcollection paths are pretty standard (something like
http://host/customers/search?params
, but still the specific search terms should go in the query string) 子集合路径是非常标准的(类似
http://host/customers/search?params
,但特定的搜索词仍应放在查询字符串中)
If on the contrary you expect to retrieve just one result by some identificator (provided for instance the customer name is unique) that's a different story, you should really use http://host/customers/:identifier
. 相反,如果您希望通过另一个标识符(例如,客户名称是唯一的)检索到一个不同的结果,则应该使用
http://host/customers/:identifier
。
In any case you can use angular resources, both parts of your application need to be aware of the routing. 无论如何,您都可以使用角度资源,应用程序的两个部分都需要了解路由。 In the front-end you define an additional verb that adds the filters (or just use the standard
query
one - see https://docs.angularjs.org/api/ngResource/service/ $resource). 在前端,您定义一个附加动词以添加过滤器(或仅使用标准
query
一个-请参阅https://docs.angularjs.org/api/ngResource/service/ $ resource)。 In the back-end you need to route the call and parse the parameters. 在后端,您需要路由呼叫并解析参数。 If it's a list, parse the query string and render your result array, if it's a single resource, parse the identifier, find the corresponding resource and render it back.
如果是列表,则解析查询字符串并呈现结果数组;如果是单个资源,则解析标识符,找到对应的资源并将其呈现回来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.