forked from readme-micro-demo/service-backend-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
86 lines (65 loc) · 2.38 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require('dotenv').config();
if (!process.env.API_KEY) {
throw new Error('Oops! The ReadMe API key is missing. Check your .env file!');
}
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const cors = require('cors');
const express = require('express');
const { MongoMemoryServer } = require('mongodb-memory-server');
const mongoose = require('mongoose');
const utils = require('./lib/utils');
MongoMemoryServer.create().then(mongoServer => {
const mongoUri = mongoServer.getUri();
mongoose.connect(mongoUri, { useNewUrlParser: true, useUnifiedTopology: true });
});
require('./lib/hoot.model');
const Hoot = mongoose.model('Hoot');
// Create some test hoots on startup so there's always some data
Hoot.create(require('./lib/fixtures'));
const app = express();
// Get project settings via ReadMe API
require('./lib/readme')(app).catch(err => {
// eslint-disable-next-line no-console
console.error(err);
process.exit(1);
});
// Receive JSON
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors());
app.use(cookieParser());
// Serve everything in /public
app.use(express.static('public'));
app.set('views', './public/views');
// Trust x-forwarded-* headers
app.set('trust proxy', true);
// Load middleware from the `/middleware` directory
require('./middleware')(app);
// Load API routes from the `/api` directory
app.use('/api', require('./api'));
// Load standard routes
app.set('view engine', 'pug');
app.get('/', (req, res) => res.redirect('/home'));
app.get('/login', (req, res) => {
// If they're already logged in, don't show login page
if (req.user) {
// if this option is included, send them back to the ReadMe docs!
if (req.query.readme) return res.redirect(res.locals.jwt);
return res.redirect('/home');
}
return res.render('login');
});
app.get('/logout', (req, res) => {
delete res.clearCookie('username');
res.redirect('/');
});
app.get('/home', (req, res) => res.render('home'));
app.get('/hoot/:id', (req, res) => res.render('home', { hoot: req.params.id }));
app.get('/@:user', (req, res) => res.render('home', { username: req.params.user }));
app.listen(utils.getPort(), () => {
// eslint-disable-next-line no-console
console.log(`Hoot app started at ${utils.getBaseUrl()}`);
// eslint-disable-next-line no-console
console.log(`Listening on port ${utils.getPort()}`);
});