Skip to content

Commit 33c1550

Browse files
committed
routing-controller-sample
0 parents  commit 33c1550

22 files changed

+940
-0
lines changed

.gitignore

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
10+
# Directory for instrumented libs generated by jscoverage/JSCover
11+
lib-cov
12+
13+
# Coverage directory used by tools like istanbul
14+
coverage
15+
16+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17+
.grunt
18+
19+
# node-waf configuration
20+
.lock-wscript
21+
22+
# Compiled binary addons (http://nodejs.org/api/addons.html)
23+
build/Release
24+
25+
# Dependency directory
26+
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
27+
node_modules
28+
29+
# Debug log from npm
30+
npm-debug.log
31+
32+
# dist folder
33+
dist
34+

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.tsdk": "./node_modules/typescript/lib"
3+
}

dist/App.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
require("reflect-metadata"); // this shim is required
3+
const routing_controllers_1 = require("routing-controllers");
4+
const express = require('express');
5+
require("./controllers/HeroController");
6+
class App {
7+
constructor() {
8+
this.express = express();
9+
routing_controllers_1.useExpressServer(this.express, {
10+
routePrefix: "/api"
11+
});
12+
// this.routes();
13+
}
14+
routes() {
15+
let router = express.Router();
16+
router.get('/', (req, res, next) => {
17+
res.json({
18+
message: "Hello world!"
19+
});
20+
});
21+
this.express.use('/', router);
22+
}
23+
}
24+
Object.defineProperty(exports, "__esModule", { value: true });
25+
exports.default = new App().express;

dist/controllers/HeroController.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"use strict";
2+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6+
return c > 3 && r && Object.defineProperty(target, key, r), r;
7+
};
8+
var __metadata = (this && this.__metadata) || function (k, v) {
9+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10+
};
11+
const typedi_1 = require("typedi");
12+
const routing_controllers_1 = require("routing-controllers");
13+
const HeroesRepository_1 = require("../repositories/HeroesRepository");
14+
let HeroController = class HeroController {
15+
constructor(heroesRepository) {
16+
this.heroesRepository = heroesRepository;
17+
this.heroesRepository = new HeroesRepository_1.HeroesRepository();
18+
}
19+
getHeroes() {
20+
return this.heroesRepository.findAll();
21+
}
22+
};
23+
__decorate([
24+
routing_controllers_1.Get("/"),
25+
routing_controllers_1.EmptyResultCode(404),
26+
__metadata('design:type', Function),
27+
__metadata('design:paramtypes', []),
28+
__metadata('design:returntype', Promise)
29+
], HeroController.prototype, "getHeroes", null);
30+
HeroController = __decorate([
31+
typedi_1.Service(),
32+
routing_controllers_1.JsonController("/v1/heroes"),
33+
__metadata('design:paramtypes', [HeroesRepository_1.HeroesRepository])
34+
], HeroController);
35+
exports.HeroController = HeroController;

dist/index.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"use strict";
2+
const http = require('http');
3+
const debug = require('debug');
4+
const App_1 = require('./App');
5+
let dubugServer = debug('MarketEve:Server');
6+
const port = normalizePort(process.env.PORT || 7746);
7+
App_1.default.set('port', port);
8+
const server = http.createServer(App_1.default);
9+
server.listen(port);
10+
server.on('listening', onListening);
11+
server.on('error', onError);
12+
function normalizePort(val) {
13+
let port = (typeof val === 'string') ? parseInt(val) : val;
14+
if (isNaN(port)) {
15+
return val;
16+
}
17+
else if (port >= 0) {
18+
return port;
19+
}
20+
else {
21+
return false;
22+
}
23+
}
24+
function onError(error) {
25+
if (error.syscall !== 'listen')
26+
throw error;
27+
let bind = (typeof port === "string") ? `Pipe ${port}` : `Port ${port}`;
28+
switch (error.code) {
29+
case 'EACCES':
30+
console.error(`${bind} requires elevated previleges`);
31+
process.exit(1);
32+
break;
33+
case 'EADDRINUSE':
34+
console.error(`${bind} is already in use`);
35+
process.exit(1);
36+
break;
37+
default:
38+
throw error;
39+
}
40+
}
41+
function onListening() {
42+
let addr = server.address();
43+
let bind = (typeof addr === 'string') ? `pipe ${addr}` : `port ${addr.port}`;
44+
dubugServer(`Listening on ${bind}`);
45+
}

dist/models/hero.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
class Height {
3+
constructor(ft, inc) {
4+
this.ft = ft;
5+
this.inc = inc;
6+
}
7+
}
8+
exports.Height = Height;
9+
class Hero {
10+
constructor(hero) {
11+
this.id = hero.id;
12+
this.name = hero.name;
13+
this.aliases = hero.aliases;
14+
this.occupation = hero.occupation;
15+
this.gender = hero.gender;
16+
this.height = new Height(hero.height.ft, hero.height.in);
17+
this.hair = hero.hair;
18+
this.eyes = hero.eyes;
19+
this.powers = hero.powers;
20+
}
21+
}
22+
exports.Hero = Hero;

dist/repositories/Hero.js

Whitespace-only changes.

dist/repositories/HeroRepository.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"use strict";
2+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6+
return c > 3 && r && Object.defineProperty(target, key, r), r;
7+
};
8+
var __metadata = (this && this.__metadata) || function (k, v) {
9+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10+
};
11+
const typedi_1 = require("typedi");
12+
const Hero_1 = require("../models/Hero");
13+
let HeroesRepository = class HeroesRepository {
14+
constructor() {
15+
this.heroes = require("./heroes");
16+
}
17+
findAll() {
18+
return this.heroes;
19+
}
20+
findById(id) {
21+
return this.heroes.find(hero => hero.id === id);
22+
}
23+
create(hero) {
24+
this.heroes.push(new Hero_1.Hero(hero));
25+
}
26+
remove(id) {
27+
const hero = this.findById(id);
28+
if (hero) {
29+
this.heroes.splice(this.heroes.indexOf(hero), 1);
30+
}
31+
return hero;
32+
}
33+
};
34+
HeroesRepository = __decorate([
35+
typedi_1.Service(),
36+
__metadata('design:paramtypes', [])
37+
], HeroesRepository);
38+
exports.HeroesRepository = HeroesRepository;

dist/repositories/HeroesRepository.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"use strict";
2+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6+
return c > 3 && r && Object.defineProperty(target, key, r), r;
7+
};
8+
var __metadata = (this && this.__metadata) || function (k, v) {
9+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10+
};
11+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
12+
return new (P || (P = Promise))(function (resolve, reject) {
13+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
14+
function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
15+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
16+
step((generator = generator.apply(thisArg, _arguments)).next());
17+
});
18+
};
19+
const typedi_1 = require("typedi");
20+
const Hero_1 = require("../models/Hero");
21+
let HeroesRepository = class HeroesRepository {
22+
constructor() {
23+
this.heroes = [];
24+
let heroes = require("./heroes").default;
25+
this.heroes = heroes.map(hero => new Hero_1.Hero(hero));
26+
}
27+
findAll() {
28+
console.log(this.heroes);
29+
return Promise.resolve(this.heroes);
30+
}
31+
findById(id) {
32+
return Promise.resolve(this.heroes.find(hero => hero.id === id));
33+
}
34+
create(hero) {
35+
this.heroes.push(new Hero_1.Hero(hero));
36+
hero.id = this.heroes.length;
37+
Promise.resolve(hero);
38+
}
39+
remove(id) {
40+
return __awaiter(this, void 0, void 0, function* () {
41+
const hero = yield this.findById(id);
42+
if (hero) {
43+
this.heroes.splice(this.heroes.indexOf(hero), 1);
44+
}
45+
return Promise.resolve(hero);
46+
});
47+
}
48+
};
49+
HeroesRepository = __decorate([
50+
typedi_1.Service(),
51+
__metadata('design:paramtypes', [])
52+
], HeroesRepository);
53+
exports.HeroesRepository = HeroesRepository;

dist/repositories/heroes.js

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
"use strict";
2+
let heroes = [
3+
{
4+
"id": 1,
5+
"name": "Luke Cage",
6+
"aliases": ["Carl Lucas", "Power Man", "Mr. Bulletproof", "Hero for Hire"],
7+
"occupation": "bartender",
8+
"gender": "male",
9+
"height": {
10+
"ft": 6,
11+
"in": 3
12+
},
13+
"hair": "bald",
14+
"eyes": "brown",
15+
"powers": [
16+
"strength",
17+
"durability",
18+
"healing"
19+
]
20+
},
21+
{
22+
"id": 2,
23+
"name": "Spider-Man",
24+
"aliases": ["Dr. Peter Benjamin Parker", "Spidey", "Web-Sligner", "Spider-X-Man"],
25+
"occupation": "scientist",
26+
"gender": "male",
27+
"height": {
28+
"ft": 5,
29+
"in": 10
30+
},
31+
"hair": "brown",
32+
"eyes": "hazel",
33+
"powers": [
34+
"wall-crawling",
35+
"strength",
36+
"speed",
37+
"stamina",
38+
"durability",
39+
"agility",
40+
"healing",
41+
"reflexes",
42+
"Spider-Sense",
43+
"genius"
44+
]
45+
},
46+
{
47+
"id": 3,
48+
"name": "Captain America",
49+
"aliases": [
50+
"Winghead",
51+
"Shield-Slinger",
52+
"the Captain",
53+
"Cap",
54+
"Yeoman America",
55+
"Sentinel of Liberty",
56+
"The Living Legend"
57+
],
58+
"occupation": "special agent",
59+
"gender": "male",
60+
"height": {
61+
"ft": 6,
62+
"in": 2
63+
},
64+
"hair": "blonde",
65+
"eyes": "blue",
66+
"powers": [
67+
"strength",
68+
"speed",
69+
"durability",
70+
"agility",
71+
"reflexes",
72+
"stamina",
73+
"healing",
74+
"longevity"
75+
]
76+
},
77+
{
78+
"id": 4,
79+
"name": "Iron Man",
80+
"aliases": [
81+
"Tony Stark",
82+
"Golden Gladiator",
83+
"Spare Parts Man",
84+
"Space-Knight"
85+
],
86+
"occupation": "inventor",
87+
"gender": "male",
88+
"height": {
89+
"ft": 6,
90+
"in": 1
91+
},
92+
"hair": "black",
93+
"eyes": "blue",
94+
"powers": []
95+
},
96+
{
97+
"id": 5,
98+
"name": "Wolverine",
99+
"aliases": [
100+
"Logan",
101+
"Weapon X",
102+
"Death",
103+
"Agent Ten",
104+
"Fist of Legend"
105+
],
106+
"occupation": "special agent",
107+
"gender": "male",
108+
"height": {
109+
"ft": 5,
110+
"in": 3
111+
},
112+
"hair": "black",
113+
"eyes": "blue",
114+
"powers": [
115+
"healing",
116+
"acute senses",
117+
"strength",
118+
"speed",
119+
"durability",
120+
"agility",
121+
"stamina",
122+
"weather adaptation",
123+
"animal empathy",
124+
"bone claws"
125+
]
126+
}
127+
];
128+
Object.defineProperty(exports, "__esModule", { value: true });
129+
exports.default = heroes;

0 commit comments

Comments
 (0)