Skip to content

Commit ec44c2f

Browse files
committed
middleware
1 parent 0bd902a commit ec44c2f

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

src/api/Router.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as express from 'express';
22
import * as bodyParser from 'body-parser';
33
import {simpleCrudRouter} from './endpoints/SimpleCrudRouter';
4+
import {middleware} from "./middleware";
45

56
// Creates and configures an ExpressJS web server.
67
class Router {
@@ -11,16 +12,10 @@ class Router {
1112
//Run configuration methods on the Express instance.
1213
constructor() {
1314
this.express = express();
14-
this.middleware();
15+
middleware.addBodyParser(this.express);
1516
this.routes();
1617
}
1718

18-
// Configure Express middleware.
19-
private middleware(): void {
20-
this.express.use(bodyParser.json());
21-
this.express.use(bodyParser.urlencoded({ extended: false }));
22-
}
23-
2419
// Configure API endpoints.
2520
private routes(): void {
2621
/* This is just to get up and running, and to make sure what we've got is

src/api/endpoints/SimpleCrudRouter.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ import {crud} from "../../db/crud";
44
export class SimpleCrudRouter {
55
router: Router;
66

7+
8+
/**
9+
* Take each handler, and attach to one of the Express.Router's
10+
* endpoints.
11+
*/
12+
init() {
13+
this.router.get('/:resource', this.getAll);
14+
this.router.get('/:resource/:id', this.getOne);
15+
}
16+
717
/**
818
* Initialize the CrudRouter
919
*/
@@ -12,6 +22,7 @@ export class SimpleCrudRouter {
1222
this.init();
1323
}
1424

25+
1526
/**
1627
* GET one resource by id
1728
*/
@@ -46,16 +57,6 @@ export class SimpleCrudRouter {
4657
}
4758

4859

49-
/**
50-
* Take each handler, and attach to one of the Express.Router's
51-
* endpoints.
52-
*/
53-
init() {
54-
this.router.get('/:resource', this.getAll);
55-
this.router.get('/:resource/:id', this.getOne);
56-
}
57-
58-
5960
}
6061

6162
// Create the CrudRouter, and export its configured Express.Router

src/api/middleware.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Configure Express middleware.
2+
3+
// Creates and configures an ExpressJS web server.
4+
import bodyParser = require("body-parser");
5+
class Middleware {
6+
7+
public addBodyParser(express): void {
8+
express.use(bodyParser.json());
9+
express.use(bodyParser.urlencoded({ extended: false }));
10+
}
11+
12+
13+
}
14+
15+
export const middleware = new Middleware();

0 commit comments

Comments
 (0)