Skip to content
This repository was archived by the owner on Oct 12, 2024. It is now read-only.

Commit da4f4b0

Browse files
committed
Setup TypeScript express routes
1 parent c070606 commit da4f4b0

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { enableProdMode } from '@angular/core';
88
import * as express from 'express';
99
import { join } from 'path';
1010
import { readFileSync } from 'fs';
11+
const api = require('./server/routes/api');
1112

1213
// Faster server renders w/ Prod mode (dev mode never needed)
1314
enableProdMode();
@@ -40,6 +41,7 @@ app.engine('html', (_, options, callback) => {
4041
});
4142
});
4243

44+
app.use('/api', api);
4345
app.set('view engine', 'html');
4446
app.set('views', join(DIST_FOLDER, 'browser'));
4547

server/routes/api.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as express from 'express';
2+
3+
class Api {
4+
public app;
5+
private message: String = 'Hello World';
6+
7+
constructor () {
8+
this.app = express();
9+
this.mountRoutes();
10+
}
11+
12+
// Access this API route using {GET} localhost:3017/api/hello
13+
private mountRoutes (): void {
14+
const router = express.Router();
15+
16+
router.get('/hello', (req, res) => {
17+
res.json({
18+
message: this.message
19+
});
20+
});
21+
22+
module.exports = router;
23+
}
24+
}
25+
26+
export default new Api().app;

0 commit comments

Comments
 (0)