This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
152 lines (123 loc) · 3.32 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Angular SSR Needed
import 'zone.js/dist/zone-node';
import { ngExpressEngine } from '@nguniversal/express-engine';
import { AppServerModule } from './src/main.server';
import { APP_BASE_HREF } from '@angular/common';
import { existsSync } from 'fs';
import { join } from 'path';
// Imports
import * as express from 'express';
import * as cors from 'cors';
import * as morgan from 'morgan';
import { Application } from 'express';
// Needed .env
import * as dotenv from 'dotenv';
import keys from './server/keys';
import { database } from 'server/database';
import { createInitialData } from 'server/users/initialData';
// Routes
import indexRoutes from './server/index/index.routes';
import wordsRoutes from './server/words/words.routes';
import categoriesRoutes from './server/categories/categories.routes';
import authRoutes from './server/auth/auth.routes';
class Server {
public app: Application;
constructor() {
// Enviroment variables
dotenv.config();
// Express
this.app = express();
this.configExpress();
this.initialConfig();
if (process.env.NODE_ENV == 'production') {
this.prodConfings();
} else {
this.devConfings();
}
// Config routes
this.routes();
}
private configExpress(): void {
this.app.set('port', process.env.PORT || keys.PORT);
this.app.use(express.json());
this.app.use(express.urlencoded({ extended: false }));
}
private prodConfings(): void {
console.log('Production mode');
// See peticions in console
this.app.use(
morgan(
'tiny', // dev - tiny
{
skip: function (req, res) {
return res.statusCode < 400;
}
}
)
);
}
private devConfings(): void {
console.log('Development mode');
// Cors policy configuration
this.app.use(cors());
// See API peticions in console
this.app.use(
morgan('dev', {
skip: function (req, res) {
return !req.baseUrl.includes('/api');
}
})
);
}
private initialConfig(): void {
database.startConnection();
createInitialData();
}
private routes(): void {
// Angular SSR
const distFolder = join(process.cwd(), 'dist/dictionaryOfNeologisms/browser');
const indexHtml = existsSync(join(distFolder, 'index.original.html'))
? 'index.original.html'
: 'index';
this.app.engine(
'html',
ngExpressEngine({
bootstrap: AppServerModule
})
);
this.app.set('view engine', 'html');
this.app.set('views', distFolder);
// API Routes
this.app.use('/api', indexRoutes);
this.app.use('/api', wordsRoutes);
this.app.use('/api', categoriesRoutes);
this.app.use('/api/auth', authRoutes);
// Serve static files from /browser
this.app.get(
'*.*',
express.static(distFolder, {
maxAge: '1y'
})
);
// All regular routes use the Universal engine
this.app.get('*', (req, res) => {
res.render(indexHtml, {
req,
providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }]
});
});
}
public start(): void {
this.app.listen(this.app.get('port'), () => {
console.log('Server on port ' + this.app.get('port'));
});
}
}
const server = new Server();
declare const __non_webpack_require__: NodeRequire;
const mainModule = __non_webpack_require__.main;
const moduleFilename = (mainModule && mainModule.filename) || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
server.start();
}
export * from './src/main.server';