Skip to content

Commit e746528

Browse files
author
Fuss Florian (uid10804)
committed
refactor(server): update example to make it easy to understand how the library works
1 parent f351a84 commit e746528

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

packages/server/example/index.ts

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
1-
import { ServerFactory, AppConfig } from '../src/index';
21
import express from 'express';
2+
import {
3+
AppConfig,
4+
Swagger,
5+
SwaggerConfig,
6+
CloudEnvironment,
7+
CoreApp,
8+
FileStorageAdapter,
9+
Environment,
10+
} from '../src/index';
311

412
const server = express();
5-
const defaultConfig = new AppConfig();
6-
defaultConfig.readOnly = false;
7-
defaultConfig.jsonFile = 'db.json';
13+
const appConfig = new AppConfig();
814

9-
const start = async () => {
10-
// do something
11-
await ServerFactory.createServer(
12-
'local',
13-
server,
14-
defaultConfig,
15-
'./package.json'
15+
const environment = new Environment();
16+
const swagger = new Swagger(
17+
server,
18+
new SwaggerConfig(appConfig.readOnly, appConfig.enableApiKeyAuth),
19+
environment.basePath,
20+
appConfig.apiRoutePath,
21+
'./package.json'
22+
);
23+
24+
let core: CoreApp | undefined;
25+
core = new CoreApp(
26+
appConfig,
27+
server,
28+
new FileStorageAdapter('db.json'),
29+
swagger,
30+
environment
31+
);
32+
33+
const init = async () => {
34+
await core!.setup();
35+
console.log(
36+
'JSON Server is running under port 3000. Use http://localhost:3000/ to access it'
1637
);
38+
server.listen(3000);
1739
};
18-
19-
start();
40+
const initPromise = init();

packages/server/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
},
1111
"scripts": {
1212
"test": "npx jest",
13-
"debug": "nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec node --inspect-brk -r ts-node/register example/index.ts",
14-
"start": "nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec 'ts-node' example/index.ts",
13+
"debug": "nodemon --watch 'src/**/*.ts' --watch 'example/index.ts' --ignore 'src/**/*.spec.ts' --exec node --inspect-brk -r ts-node/register example/index.ts",
14+
"start": "nodemon --watch 'src/**/*.ts' --watch 'example/index.ts' --ignore 'src/**/*.spec.ts' --exec 'ts-node' example/index.ts",
1515
"type-check": "tsc --noEmit",
1616
"type-check:watch": "npm run type-check -- --watch",
1717
"build": "tsc -b",

0 commit comments

Comments
 (0)