简体   繁体   English

expressJS路由器规划-如何设置有效的路由器

[英]expressJS router planning - how to set up an effective router

I realize that this may be a fairly simple question but bear with me. 我意识到这可能是一个非常简单的问题,但请耐心等待。 I am really new to node/express. 我真的是节点/表达的新手。

My directory structure looks like: 我的目录结构如下:

auth
    index.html
pub
    index.html
    login.html

the idea here is that the resources in pub are publicly available but the resources in auth are only available after a user is authenticated. 这里的想法是pub中的资源是公开可用的,但是auth中的资源仅在对用户进行身份验证之后才可用。

However, at this point, I am just trying to get these pages to come back properly from the server. 但是,在这一点上,我只是想让这些页面从服务器正确返回。 Ideally, my routing engine would be able to serve these pages up based on some parameter. 理想情况下,我的路由引擎将能够基于某些参数来提供这些页面。 So: 所以:

site.com -> pub/index.html
site.com/login/ -> pub/login.html
site.com/dashboard/ -> auth/index.html

I tried something like this: 我尝试过这样的事情:

router.get('/dashboard/', function(req, res, next) {
    res.sendFile(__dirname + "/src/auth/index.html");
});

router.get('/login/', function(req, res, next) {
    res.sendFile(__dirname + "/src/pub/login.html");
});
router.get('*', function(req, res, next) {
    res.sendFile(__dirname + "/src/pub/index.html");
});

However, the problem I quickly found was that these pages are requesting resources relative to their own position in the directory structure and all requests were being returned the default index.html. 但是,我很快发现的问题是这些页面正在请求相对于其在目录结构中的位置的资源,并且所有请求都被返回为默认的index.html。 So, for example if I type site.com in the browser index.html loads. 因此,例如,如果我在浏览器中键入site.com index.html加载。 Here is part of index.html: 这是index.html的一部分:

<script src="js/jquery.min.js"></script>

naturally then, the browser makes another request for /js/jquery.min.js which the router can't find so it responds with index.html again. 然后,浏览器自然会向路由器找不到/js/jquery.min.js的另一个请求,因此它将再次使用index.html进行响应。

How do I design a routing engine that is smart enough to find the correct view based on the url and then understand that it needs to serve all requests from that page relative to that pages position in the directory structure? 如何设计一个足够聪明的路由引擎,以便根据url找到正确的视图,然后理解它需要服务于该页面相对于目录结构中该页面位置的所有请求? Or is there another standard way of handling this kind of problem? 还是有另一种处理此类问题的标准方法?

To complicate matters, the auth/index.html is an angular page. 使事情变得复杂的是,auth / index.html是一个有角度的页面。 So, once it loads it will be requesting all kinds of html pages relative to its position in the directory structure (depending on routes and included templates etc.) 因此,一旦加载,它将请求相对于其在目录结构中位置的各种html页面(取决于路由和所包含的模板等)。

Thanks in advance. 提前致谢。

Those are a lot of questions but I think I can at least get you pointed in the right direction :) 这些问题很多,但我想至少可以使您指出正确的方向:)

However, at this point, I am just trying to get these pages to come back properly from the server. 但是,在这一点上,我只是想让这些页面从服务器正确返回。

To do this with express, you can use express.static to designate a public directory whose assets get made available to web requests. 为此,可以使用express.static来指定一个公共目录,该目录的资产可用于Web请求。 For example, if you had a directory sturcture like this: 例如,如果您有这样的目录结构:

public/
  templates/
    index.html
  stylesheets/
  js/
    jquery.min.js

In express, you would do this: 明确地说,您可以这样做:

app.use(express.static(__dirname + '/public'));

in order to expose those files as static assets, relative to the public dir, eg http://yourserver.com/templates/index.html 为了将这些文件作为相对于公共目录的静态资产公开,例如http://yourserver.com/templates/index.html

To complicate matters, the auth/index.html is an angular page. 使事情变得复杂的是,auth / index.html是一个有角度的页面。 So, once it loads it will be requesting all kinds of html pages relative to its position in the directory structure 因此,一旦加载,它将请求各种与其位置在目录结构中相关的html页面

I think part of your confusion here is knowing the difference between client side routing and server side routing in an AngularJS/node.js app. 我认为您对此感到困惑的一部分是知道AngularJS / node.js应用程序中客户端路由和服务器端路由之间的区别。

AngularJS is a framework for building single page apps (SPA). AngularJS是用于构建单页应用程序(SPA)的框架。 What this means is your browser requests one HTML file at the start (eg an index.html served from the route '/' on your server) to get things started, which loads some bootstraping javascript. 这意味着您的浏览器在开始时请求一个HTML文件(例如,从服务器上的路由“ /”提供的index.html)开始操作,从而加载了一些引导Javascript。 From then on, client side javascript and AJAX calls will handle all of the rest to facilitate rendering additional HTML, user interaction, and navigation to other parts of your app. 从那时起,客户端的javascript和AJAX调用将处理所有其余部分,以帮助呈现其他HTML,用户交互以及导航到应用程序其他部分的方式。 The URL in the browser will change, but you'll notice that no further page reloads will take place as you navigate. 浏览器中的URL将会更改,但是您会注意到在浏览时不会再进行页面重新加载。 This is the client side routing that you can use AngularJS to build. 这是可以使用AngularJS构建的客户端路由。 If you've looked at the AngularJS tutorial , step 7 goes over how this works. 如果您已经看过AngularJS教程 ,则第7步将介绍它的工作原理。

Your server side routes are typically not involved in this page navigation. 您的服务器端路由通常不参与此页面导航。 Instead, your server should provide an API for the AngularJS client side will mae AJAX calls to for creating, reading, updating, deleting (CRUD) application data. 相反,您的服务器应该为AngularJS客户端提供API,以便AAAX调用以创建,读取,更新,删除(CRUD)应用程序数据。 For login for example, you could have a server side /api/login route that doesn't return an HTML page, but rather accepts a username and password via a POST request, establishes some session state, and then returns the result to be dealt with on the client side. 例如,对于登录,您可能有一个服务器端/api/login路由,该路由不返回HTML页面,而是通过POST请求接受用户名和密码,建立一些会话状态,然后返回要处理的结果在客户端。

In addition to the AngularJS tutorials, I would invite you to take a look at mean.js for an end to end example of what a node.js + angularJS app looks like. 除了AngularJS教程之外,我还邀请您看一下mean.js ,作为node.js + angularJS应用程序外观的端到端示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM