當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript Router.all方法代碼示例

本文整理匯總了TypeScript中express.Router.all方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Router.all方法的具體用法?TypeScript Router.all怎麽用?TypeScript Router.all使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在express.Router的用法示例。


在下文中一共展示了Router.all方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: function

export default function (): Router {

    var router: Router = Router();

    var authenticate = (req: Request, res: Response, next: any) => {
        if (!req.session["user_id"]) {
            DKYSession.raiseMessage(req.session, "danger", "You are not logged in.");
            res.redirect("/login");
        } else {
            next();
        }
    }

    // Routes.
    router.all(["/", "/label/:label?", "/p/:p?", "/label/:label?/p/:p?"], (req: Request, res: Response, next: any) => {
        defaultRoutes.index(req, res).catch(next);
    });
    router.get("/login", (req: Request, res: Response, next: any) => {
        defaultRoutes.getLogin(req, res).catch(next);
    });
    router.post("/login", (req: Request, res: Response, next: any) => {
        defaultRoutes.login(req, res).catch(next);
    });
    router.get("/logout", authenticate, (req: Request, res: Response, next: any) => {
        defaultRoutes.getLogout(req, res).catch(next);
    });
    router.post("/logout", authenticate, (req: Request, res: Response, next: any) => {
        defaultRoutes.logout(req, res).catch(next);
    });
    router.all("/post/view/:id?", (req: Request, res: Response, next: any) => {
        postRoutes.view(req, res).catch(next);
    });
    router.all("/post/:action?/:id?", authenticate, (req: Request, res: Response, next: any) => {
        postRoutes.post(req, res).catch(next);
    });
    router.all("/:alias", (req: Request, res: Response, next: any) => {
        postRoutes.view(req, res).catch(next);
    });
    // Catch all.
    router.all("*", (req: Request, res: Response, next: any) => {
        defaultRoutes.error404(req, res).catch(next);
    });

    return router;
}
開發者ID:nicholas-robson,項目名稱:dkydev_webapp,代碼行數:45,代碼來源:router.ts

示例2: mount

  mount(router: Router) {
    var controller = require(this.controller)

    // if there is a target method, check to see if it exists and replace the original controller with it
    if (this.target) {
      if (!controller.hasOwnProperty(this.target)) {
        throw new Error("target function " + this.target + " does not exist on controller " + this.controller)
      }
      controller = controller[this.target]
    }

    // if controller is not a function, throw an error
    if (typeof (controller) != "function") {
      throw new Error("controllers must resolve to a function")
    }

    // use a switch (rather than checking for the method) to prevent 
    // unwanted method calls (ie. router.route(this.path, controller) would not be good...)
    switch (this.method.toLowerCase()) {
      case "all":
        router.all(this.path, controller)
        break
      case "get":
        router.get(this.path, controller)
        break
      case "put":
        router.put(this.path, controller)
        break
      case "post":
        router.post(this.path, controller)
        break
      case "delete":
        router.delete(this.path, controller)
        break
      case "patch":
        router.patch(this.path, controller)
        break
      case "use":
        if (typeof this.path === "string") {
          router.use(this.path, controller);
        } else {
          router.use(controller);
        }
        break;
      case "param":
        if (typeof this.path === "string") {
          router.param(this.path, controller);
        } else {
          router.param(controller);
        }
        break;
      default:
        throw new Error("unsupported method " + this.method)
    }
  }
開發者ID:Beng89,項目名稱:adv-route,代碼行數:55,代碼來源:route.ts


注:本文中的express.Router.all方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。