技术控

    今日:136| 主题:49228
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Express-Torch: Expressive and maintainable routes for Express.js

[复制链接]
△ 落殇 发表于 3 天前
45 2

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
Express Torch

  Expressive and maintainable routes for Express.js.
  Running the example

  The project includes a simple example rendering a    /and    /api/postsendpoint, you can set it up as follows:  
  1. $ cd ./example
  2. $ yarn
  3. $ npm start
复制代码
Now visit    http://localhost:3000and take a look at    server.js!  
  Usage

  Basic usage

  You pass your Express app to Torch, and a callback in which you can specify your routes.
  1. import Express from 'express';
  2. import Torch from 'torch';
  3. import HomeController from './controllers/home';
  4. const app = Express();
  5. Torch(app, (router) => {
  6.     router.get('/', HomeController.index);
  7. });
  8. //... your other Express logic
  9. app.listen(3000);
复制代码
Grouping

  This is where Torch shines, you can register a bunch of routes that require all parameters of the groups above, such as a    prefixfor your    /apiroutes.  
  1. import Express from 'express';
  2. import Torch from 'torch';
  3. import HomeController from './controllers/home';
  4. const app = Express();
  5. Torch(app, (router) => {
  6.     router.get('/', HomeController.index);
  7.     router.group({prefix: 'api'}, (router) => {
  8.         router.get('/posts', HomeController.index);// will evaluate to /api/posts
  9.         router.get('/posts/:id', HomeController.show);// will evaluate to /api/posts/:id
  10.     });
  11. });
  12. //... your other Express logic
  13. app.listen(3000);
复制代码
Middleware

  Groups have another advantage, you can apply regular Express middleware to them, so that they stack together without you having to specify them per route
  1. import Express from 'express';
  2. import Torch from 'torch';
  3. import HomeController from './controllers/home';
  4. const app = Express();
  5. Torch(app, (router) => {
  6.     router.group({middlware: [Cookies, Session]}, (router) => {
  7.     router.get('/', HomeController.index);// Cookies > Session
  8.     router.group({middleware: [Throttle]}, (router) => {
  9.         router.get('/posts', HomeController.index);// Cookies > Session > Throttle
  10.         router.group({middleware: [OAuth, Policy('manage-posts')]}, (router) => {
  11.             router.get('/posts/:id', HomeController.show);// Cookies > Session > Throttle > OAuth > Policy('manage-posts')
  12.         });
  13.     });
  14. });
  15. //... your other Express logic
  16. app.listen(3000);
复制代码
if you want, you can still apply middleware to a single route
  1. import Express from 'express';
  2. import Torch from 'torch';
  3. import HomeController from './controllers/home';
  4. const app = Express();
  5. Torch(app, (router) => {
  6.     router.get('/', {,
  7.         middleware: [Auth],
  8.         controller: HomeController.index
  9.     });
  10. });
  11. //... your other Express logic
  12. app.listen(3000);
复制代码
Naming

  Since urls can be subject to your client's requests you don't want to spread them all around your application as if they were static. Torch allows you to add a mapping so that some    namewill map to a path.  
  1. import Express from 'express';
  2. import Torch from 'torch';
  3. import HomeController from './controllers/home';
  4. const app = Express();
  5. const routes = Torch(app, (router) => {
  6.     router.group({prefix: '/api'}, function(router) {
  7.         router.get('/posts/:id', {,
  8.             name: 'api.posts.show',
  9.             controller: HomeController.index
  10.         });
  11.     });
  12. });
  13. routes.named('api.posts.show',  { id: 123 });// will evaluate to /api/posts/123
  14. //... your other Express logic
  15. app.listen(3000);
  16. /**** in ./controllers/home/.js ****/
  17. {
  18.     index: (req, res) => {
  19.         req.routes.named('api.posts.show', {id: 123})
  20.     }
  21. }
复制代码
友荐云推荐




上一篇:redux-subscriber - Subscribe to changes in any part of redux state
下一篇:GUI for Data Modeling with Elasticsearch
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

Lee_Fun 发表于 前天 07:48
占位编辑
回复 支持 反对

使用道具 举报

张远强 发表于 前天 17:31
nO Zuo nO Die
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表