Skip to content

Commit 0fb9f60

Browse files
committed
🚧 app function converted to class es6
1 parent b678fab commit 0fb9f60

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

src/app.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
import './env';
2+
import './database';
23
import express from 'express';
34
import bodyParser from 'body-parser';
45
import helmet from 'helmet';
56
import morgan from 'morgan';
67
import routes from './routes';
78
import { loggerMiddleware } from './middlewares/logger';
8-
import database from './database';
99

10-
const prepare = () => {
11-
const app = express();
10+
class App {
11+
constructor() {
12+
this.server = express();
13+
this.middlewares();
14+
this.routes();
15+
}
1216

13-
app.use(bodyParser.json());
14-
app.use(morgan('combined'));
15-
app.use(helmet());
16-
app.use(loggerMiddleware);
17+
middlewares() {
18+
this.server.use(bodyParser.json());
19+
this.server.use(morgan('combined'));
20+
this.server.use(helmet());
21+
this.server.use(loggerMiddleware);
22+
}
1723

18-
app.use('/', routes);
24+
routes() {
25+
this.server.use(routes);
26+
}
27+
}
1928

20-
app.database = database;
21-
22-
return app;
23-
};
24-
25-
export default async () => {
26-
const app = prepare();
27-
await app.database.connect();
28-
29-
return app;
30-
};
29+
export default new App().server;

src/server.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import setup from './app';
1+
import app from './app';
22

33
const port = process.env.PORT || 3000;
44

5-
(async () => {
6-
try {
7-
const app = await setup();
8-
app.listen(port, () => console.info(`Listening on port ${port}`));
9-
} catch (error) {
10-
console.error(error);
11-
process.exit(1);
12-
}
13-
})();
5+
try {
6+
app.listen(port, () => console.info(`Listening on port ${port}`));
7+
} catch (error) {
8+
console.error(error);
9+
process.exit(1);
10+
}

0 commit comments

Comments
 (0)