Express中的路由处理程序是同步调用还是异步调用?(Are the route handlers in express called synchronously or asynchronously?)

我在routes.js文件中有两个重定向:

app.get('*', blockingController01); app.get('/test', controller02);

业务是,我有一个“阻止登陆页面”,您必须输入有效代码才能访问该网站的其余部分。 blockingController01发送带有表单的呈现页面以输入代码。 如果用户没有输入正确的代码,那么如果他/她输入'/ test'或任何其他页面应该重定向到insert code页。

我试图解决这个问题,只需在开头为代码页设置一个控制器,并用通配符*覆盖所有路径。 所以我想知道两件事:

是否异步调用处理相同路径的控制器? 快递有什么可以避免调用其余的控制器吗?

谢谢!

I have two redirections in a routes.js file:

app.get('*', blockingController01); app.get('/test', controller02);

The business is, that I have a 'blocking landing page' in which you have to enter a valid code to access to the rest of the site. blockingController01 sends a rendered page with a form to enter the code. If user didn't enter the a correct code then if he/she enters '/test' or any other the page should redirect to the insert code page.

I tried to solve this just putting a controller for the code page at the beginning and covering all paths with the wildcard *. So i'm wondering two things:

Are the controllers that handle the same paths called asynchronously? Does express have something to avoid to call the rest of the controllers?

Thanks!

最满意答案

控制器(路由处理程序)不会同时调用(我认为你的意思是“异步”)。

它们按照它们的定义被调用,因此在你的情况下,将为所有GET请求调用blockingController01 。

如果令牌正确,该控制器可以将请求传递给与URL匹配的其他路由处理程序。

这是您尝试做的一个非常基本的例子:

app.get('*', (req, res, next) => { if (req.query.token !== '12345') { // You would use `res.render()` here, this is just a quick demo: return res.send(` <form> Please enter a token and press enter: <input type=text name=token> </form> `); } next(); }); app.get('/test', (req, res) => { res.send('<h1>Correct token!</h1>'); });

所以任何GET请求都会命中第一个路由处理程序,它检查令牌的有效性(在这种情况下,它只检查查询字符串参数token是否具有值“12345” 。如果不是,它将呈现一个表单,但是如果令牌匹配,它将调用next() ,它将请求传递给第二个路由处理程序。

Controllers (route handlers) are not called concurrently (which is what I think you mean with "asynchronously").

They are called in order of their definition, so in your case blockingController01 will be called for all GET requests.

That controller can pass the request along, if the token is correct, to other route handlers that match the URL.

Here's a very basic example of what you're trying to do:

app.get('*', (req, res, next) => { if (req.query.token !== '12345') { // You would use `res.render()` here, this is just a quick demo: return res.send(` <form> Please enter a token and press enter: <input type=text name=token> </form> `); } next(); }); app.get('/test', (req, res) => { res.send('<h1>Correct token!</h1>'); });

So any GET request will hit the first route handler, which checks the validity of the token (in this case, it just checks if the query string parameter token has a value of "12345". If not, it will render a form, but if the token matches it will call next() which passes the request to the second route handler.

Express中的路由处理程序是同步调用还是异步调用?(Are the route handlers in express called synchronously or asynchronously?)

我在routes.js文件中有两个重定向:

app.get('*', blockingController01); app.get('/test', controller02);

业务是,我有一个“阻止登陆页面”,您必须输入有效代码才能访问该网站的其余部分。 blockingController01发送带有表单的呈现页面以输入代码。 如果用户没有输入正确的代码,那么如果他/她输入'/ test'或任何其他页面应该重定向到insert code页。

我试图解决这个问题,只需在开头为代码页设置一个控制器,并用通配符*覆盖所有路径。 所以我想知道两件事:

是否异步调用处理相同路径的控制器? 快递有什么可以避免调用其余的控制器吗?

谢谢!

I have two redirections in a routes.js file:

app.get('*', blockingController01); app.get('/test', controller02);

The business is, that I have a 'blocking landing page' in which you have to enter a valid code to access to the rest of the site. blockingController01 sends a rendered page with a form to enter the code. If user didn't enter the a correct code then if he/she enters '/test' or any other the page should redirect to the insert code page.

I tried to solve this just putting a controller for the code page at the beginning and covering all paths with the wildcard *. So i'm wondering two things:

Are the controllers that handle the same paths called asynchronously? Does express have something to avoid to call the rest of the controllers?

Thanks!

最满意答案

控制器(路由处理程序)不会同时调用(我认为你的意思是“异步”)。

它们按照它们的定义被调用,因此在你的情况下,将为所有GET请求调用blockingController01 。

如果令牌正确,该控制器可以将请求传递给与URL匹配的其他路由处理程序。

这是您尝试做的一个非常基本的例子:

app.get('*', (req, res, next) => { if (req.query.token !== '12345') { // You would use `res.render()` here, this is just a quick demo: return res.send(` <form> Please enter a token and press enter: <input type=text name=token> </form> `); } next(); }); app.get('/test', (req, res) => { res.send('<h1>Correct token!</h1>'); });

所以任何GET请求都会命中第一个路由处理程序,它检查令牌的有效性(在这种情况下,它只检查查询字符串参数token是否具有值“12345” 。如果不是,它将呈现一个表单,但是如果令牌匹配,它将调用next() ,它将请求传递给第二个路由处理程序。

Controllers (route handlers) are not called concurrently (which is what I think you mean with "asynchronously").

They are called in order of their definition, so in your case blockingController01 will be called for all GET requests.

That controller can pass the request along, if the token is correct, to other route handlers that match the URL.

Here's a very basic example of what you're trying to do:

app.get('*', (req, res, next) => { if (req.query.token !== '12345') { // You would use `res.render()` here, this is just a quick demo: return res.send(` <form> Please enter a token and press enter: <input type=text name=token> </form> `); } next(); }); app.get('/test', (req, res) => { res.send('<h1>Correct token!</h1>'); });

So any GET request will hit the first route handler, which checks the validity of the token (in this case, it just checks if the query string parameter token has a value of "12345". If not, it will render a form, but if the token matches it will call next() which passes the request to the second route handler.