-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathapp.js
54 lines (43 loc) · 1.71 KB
/
app.js
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
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const pino = require('pino');
const pinoHttp = require('pino-http');
const indexRouter = require('./routes/index');
const imageRouter = require('./routes/image');
const pingRouter = require('./routes/ping');
// [<snippet talkers-router-import>]
const talkersRouter = require('./routes/talkers');
// [<endsnippet talkers-router-import>]
const logger = pino({ level: process.env.LOG_LEVEL || 'info' });
const db = require('./db/models')((msg) => logger.debug(msg));
const app = express();
if (process.env.NODE_ENV != 'production') {
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const config = require('./webpack.config.js');
const compiler = webpack(config);
app.use(
webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath,
})
);
}
app.use(pinoHttp({ logger, useLevel: 'info' }));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/image', imageRouter);
app.use('/ping', pingRouter);
// [<snippet talkers-router-add>]
app.use('/', talkersRouter(db));
// [<endsnippet talkers-router-add>]
// [<en>] Error handler helps to avoid raw stack traces on logs and to use the logger of choise.
// [<ru>] Обработчик ошибок поможет избежать «сырых» трейсов в логе и использовать выбранный логгер.
app.use((err, req, res, next) => {
req.log.error(err);
res.status(500).send({ body: err.message });
});
module.exports = app;