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


TypeScript Router.use方法代碼示例

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


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

示例1: setDummyRouting

function setDummyRouting(app: Express){
    let dummyRouter: Router = Router();

    let dummyServePath: string = path.join(__dirname, '../public/views/dummy');
    dummyRouter.use( express.static(dummyServePath) );

    dummyRouter.get('/test', function(req : Request, res: Response){
        res.send(dummy.getDummyMessage());
    })

    dummyRouter.get('/forajax', function(req : Request, res: Response){
        res.send('This was retrieved through ajax!');
    })

    app.use('/dummy/', dummyRouter);
}
開發者ID:peerhenry,項目名稱:pulety,代碼行數:16,代碼來源:routeconfig.ts

示例2: router

 async router(base:Router, options?: RouterOptions): Promise<Router> {
   let r = Router(options);
   let knownPaths = new Set();
   let promises: Promise<Router>[] = [];
   this.operations.forEach((operation: Operation) => {
     knownPaths.add(operation.path);
     promises.push(operation.router(r));
   });
   await Promise.all(promises);
   knownPaths.forEach(path => {
     r.all(path, (req: APIRequest, res: APIResponse, next: NextFunction) => {
       next(API.newError(405, 'Method Not Allowed', "The API Endpoint doesn't support the specified HTTP method for the given resource"));
     });
   });
   base.use(this.basePath, r);
   return r;
 }
開發者ID:vivocha,項目名稱:arrest,代碼行數:17,代碼來源:resource.ts

示例3: registerInterceptor

 private registerInterceptor(interceptor: Interceptor) {
     this.router.use((request:Request, response: Response, next: NextFunction) => {
         interceptor.preHandle(request, response)
             .then(next).catch((error) => console.log(error));
     });
 }
開發者ID:PesanskiGoran,項目名稱:framework,代碼行數:6,代碼來源:Dispatcher.ts

示例4: require

/**
 * Created by Franz on 4/30/2016.
 */
import express = require('express');
import {Router} from "express";
import securityRouter from './securityRouter';
import someThingRouter from './someThingRouter';

let topRouter:Router = express.Router();

topRouter.use('/api/security', securityRouter);
topRouter.use('/api/something', someThingRouter);

export default topRouter;
開發者ID:FranzZemen,項目名稱:nodets-scaffolding,代碼行數:14,代碼來源:topRouter.ts

示例5: Router

import { Router, Response } from "express";
import { verify } from "jsonwebtoken";
import { secret } from "../config";

const protectedRouter: Router = Router();

protectedRouter.use(function(req: any, res: Response, next) {
    const token = req.headers.auth;
    verify(token, secret, function(tokenError) {
        if (tokenError) {
            return res.status(403).json({
                message: "Invalid token, please Log in first"
            });
        }

        next();
    });
});

protectedRouter.get("/", function(req, res) {
    res.json({
        text: "Greetings, you have valid token.",
        title: "Protected call"
    });
});

export {protectedRouter}



開發者ID:DevEngage,項目名稱:angular2-express-starter,代碼行數:27,代碼來源:protected.ts

示例6: Router

import { Router, Response, Request, NextFunction } from 'express';
import { verify } from 'jsonwebtoken';
import { secret } from '../config';

const protectedRouter: Router = Router();

protectedRouter.use((request: Request & { headers: { authorization: string } }, response: Response, next: NextFunction) => {
    const token = request.headers.authorization;

    verify(token, secret, function(tokenError) {
        if (tokenError) {
            return response.status(403).json({
                message: 'Invalid token, please Log in first'
            });
        }

        next();
    });
});

protectedRouter.get('/', (request: Request, response: Response) => {
    response.json({
        text: 'Greetings, you have valid token.',
        title: 'Protected call'
    });
});

export { protectedRouter }


開發者ID:akazimirov,項目名稱:domic,代碼行數:28,代碼來源:protected.ts

示例7: require

import * as async from 'async';
import * as bcrypt from 'bcrypt';
import {Router, Response} from 'express';
import {NextFunction} from 'express';
import jsonApiSerializer = require('jsonapi-serializer');
import {IUser, User} from '../models/user';
import {Authentication} from '../authentication';
import {userSerializer} from '../serializers/user-serializer';
import {IRequest} from '../interfaces';

namespace UserRoutes {
  'use strict';

  export const router: Router = Router();
  router.use(function (req, res, next) {
    console.log('this is just an empty middleware');
    next();
  });
  router.post('/', function (req: IRequest, res: Response, next: NextFunction): void {
    console.log('let us create a user please');
    // validate incoming data:
    // we need a user name of min 6 char long
    req.checkBody('data.type', 'not a user record').equals('users');
    req.checkBody('data.attributes.name', 'not alphanumeric').isAlphanumeric();
    req.checkBody('data.attributes.name', 'too short (3 char min)').len(3, undefined);
    // we need an email that is a proper email
    req.checkBody('data.attributes.email', 'invalid email').isEmail();
    // we need a password that is at least 6 char long
    req.checkBody('data.attributes.password', 'password too short  (6 char min)').len(6, undefined);

    let errors: Dictionary<any> = req.validationErrors(true);
    // if any of these parameter does not fit the criteria
開發者ID:dadakoko,項目名稱:play-server,代碼行數:32,代碼來源:users.ts

示例8: Router

import { NextFunction, Request, Response, Router } from "express";
import { verify } from "jsonwebtoken";
import { secret } from "../config";

const protectedRouter: Router = Router();

type AuthorizedRequest = Request & { headers: { authorization: string } };

protectedRouter.use((request: AuthorizedRequest, response: Response, next: NextFunction) => {
    const token = request.headers.authorization;

    verify(token, secret, (tokenError) => {
        if (tokenError) {
            return response.status(403).json({
                message: "Invalid token, please Log in first",
            });
        }

        next();
    });
});

protectedRouter.get("/", (request: Request, response: Response) => {
    response.json({
        text: "Greetings, you have valid token.",
        title: "Protected call",
    });
});

export { protectedRouter };
開發者ID:venki143mca,項目名稱:TeamBirthday,代碼行數:30,代碼來源:protected.ts

示例9: Router

import { Request, Response, Router } from "express";
import { userRoutes } from "./user.routes";

const apiRoutes: Router = Router();

apiRoutes.get("/pop", (req, res, next) => {
    // This isn't part of API and is just used from a browser or curl to test that
    // "/pop" is being routed correctly.

    const testObject = {
        "AppName": "MongoPop",
        "Version": 1.0
    }
    res.json(testObject);
});

apiRoutes.use("/users", userRoutes);

export { apiRoutes };
開發者ID:relevantmedia,項目名稱:angular2_mean,代碼行數:19,代碼來源:api.routes.ts

示例10: Router

//ver https://github.com/expressjs/session y https://github.com/vladotesanovic/angular2-express-starter/tree/master/server/routes
import { Router, Response, Request } from "express";
import { } from "express-session"

const protectedRouter: Router = Router();

protectedRouter.use(function(req: Request, res: Response, next) {
    if(req.session){
        next();
    }
    //redirect("/login")
});

export { protectedRouter }





開發者ID:hernan91,項目名稱:ww3,代碼行數:14,代碼來源:protected.ts


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