-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
47 lines (39 loc) · 1.28 KB
/
app.js
File metadata and controls
47 lines (39 loc) · 1.28 KB
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
require('dotenv').config(); // считываем необходимые переменные окружения
const PORT = process.env.PORT || 3000; // порт сервера
const path = require('path');
const bodyParser = require('body-parser');
const cors = require('cors');
const express = require('express');
const app = express();
const helmet = require('helmet');
app.use(helmet());
// парсинг post запросов от клиента
app.use(bodyParser.urlencoded({ extended: true })); // ключ: значение
app.use(bodyParser.json());
// статика
app.use(express.static(path.join(__dirname, 'public')));
// роутер
app.use(cors());
app.use('/', require('./routes'));
// основной сервер
app.listen(PORT, () => {
console.log('CORS-enabled web server');
console.log(`Сервер запущен на порту ${PORT}`);
});
// error handler
app.use((err, req, res, next) => {
if (err) {
console.error(err.stack);
res.status(500).send('Something broke!');
} else {
next();
}
});
// в случае неопределенной ошибки
process.on('uncaughtException', (err) => {
console.error(
`${new Date().toUTCString()} uncaught exception: ${err.message}`
);
console.error(err.stack);
process.exit(1);
});