-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (37 loc) · 1.35 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
var config = require('./config');
// Setup server
var express = require('express');
var app = express();
app.set('token_key', config.token_key);
var port = process.env.PORT || 8080;
var bodyParser = require('body-parser');
var assert = require('assert');
var path = require('path');
app.use(express.static(__dirname + '/dist'));
app.listen(port);
console.log("Listening on port " + port);
// Connect to database
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect(config.db_url, {useMongoClient: true});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'Connection to database failed'));
db.once('open', function() {
console.log('Connection to database successful');
});
// Handle routing
auth = require('./app/authentication')(app);
var authRouter = require('./app/routes')(app);
var notesRouter = require('./app/routes.notes');
var usersRouter = require('./app/routes.users');
var updownloadRouter = require('./app/routes.updownloads');
app.use('/api/notes', auth, notesRouter);
app.use('/api/users', auth, usersRouter);
app.use('/api/uploads', auth, updownloadRouter);
app.use('/api', authRouter);
app.get('/uploads/*', (req, res) => {
res.sendFile(path.join(__dirname, 'data/uploads/' + req.params[0]));
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});