本文整理汇总了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);
}
示例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;
}
示例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));
});
}
示例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;
示例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}
示例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 }
示例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
示例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 };
示例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 };
示例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 }