forked from andela/mazus-ah-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (55 loc) · 1.71 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import '@babel/polyfill';
import express from 'express';
import cors from 'cors';
import logger from 'morgan';
import Debug from 'debug';
import swaggerUi from 'swagger-ui-express';
import passport from 'passport';
import docs from './docs';
import routes from './routes';
import ServerResponse from './modules';
import models from './database/models';
const debug = Debug('dev');
const isProduction = process.env.NODE_ENV === 'production';
const { notFoundError, developmentServerErrorResponse, serverErrorResponse } = ServerResponse;
const { sequelize } = models;
const API_PREFIX = '/api/v1';
// Create global app object
const app = express();
app.use(cors());
// Normal express config defaults
app.use(logger('dev'));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(express.static(`${__dirname}/public`));
app.use(passport.initialize());
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
done(null, user);
});
app.use(API_PREFIX, routes);
app.get('/', (req, res) => {
res.status(200).json({ message: "Welcome to Author's Haven" });
});
app.use('/api/v1/docs', swaggerUi.serve, swaggerUi.setup(docs));
// catch 404 and forward to error handler
app.use(notFoundError);
// development error handler
// will print stacktrace
/* istanbul ignore next-line */
if (!isProduction) {
app.use(developmentServerErrorResponse);
}
// production error handler
// no stacktraces leaked to user
app.use(serverErrorResponse);
(async () => {
await sequelize.sync();
})();
// finally, let's start our server...
const server = app.listen(process.env.PORT || 3000, () => {
debug(`Listening on port ${server.address().port}`);
});
export default app;