-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathparams.ts
36 lines (29 loc) · 1.12 KB
/
params.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// using req.params
import express = require('express');
import {Request, Response, NextFunction} from 'express';
const app: express.Application = express();
// using params as hash
app.post('/whatever/:aParam', (req: Request, res: Response, next: NextFunction) => {
res.send(req.params['aParam']);
});
// specifying the params as an interface
interface WhateverParams {
params: {
aParam: string;
};
}
app.post('/whatever/:aParam', (req: Request & WhateverParams, res: Response, next: NextFunction) => {
res.send(req.params.aParam);
// accessing a non-existend param would now throw a compile-time error:
// res.send(req.params.anotherParam);
});
// specifying what's available inline
app.post('/whatever/:aParam', (req: Request & {params: {aParam: string}}, res: Response, next: NextFunction) => {
res.send(req.params.aParam);
// accessing a non-existend param would now throw a compile-time error:
// res.send(req.params.anotherParam);
});
// regular expression matching group
app.get(/hello(.*)world/, (req: Request, res: Response, next: NextFunction) => {
res.send(req.params[1]);
});