-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
58 lines (47 loc) · 1.55 KB
/
server.js
File metadata and controls
58 lines (47 loc) · 1.55 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
48
49
50
51
52
53
54
55
56
57
58
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const swaggerUi = require('swagger-ui-express');
const yamljs = require('yamljs');
const fs = require('fs');
const path = require('path');
const config = require('./config');
const passportConfig = require('./src/auth/passport-config');
const passport = require('./src/passport');
const cookieParser = require('cookie-parser');
// Set mongoose.Promise to any Promise implementation
mongoose.Promise = Promise;
// instantiate express
const app = express();
app.use(cookieParser());
// connect to Mongo
mongoose.connect(config.DATABASE_URI + config.DATABASE_COLLECTION_AUTH, { useNewUrlParser: true });
// Serve documentation
const spec = yamljs.load('./docs/docs.yaml');
app.use('/docs', swaggerUi.serve, swaggerUi.setup(spec));
// Setup favicon
app.get('/auth/docs/favicon.ico', (req, res) => {
res.sendFile(path.join(__dirname, '/docs/favicon.ico'));
});
// setup passport
app.use(passport.initialize());
// setup express middlewares
app.use(cors({
origin: config.HOST,
credentials: true
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// unprotected routing
const router = require('./src/routes/router')(passport);
app.use('/auth', router);
// catch all middleware
app.use((req, res) => {
res.status(404).json({ error: 'Not Found' });
});
// start the server
const PORT = process.env.PORT || 3002;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});