-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
37 lines (28 loc) · 1.01 KB
/
server.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
// pokedex v0.1.0-alpha, whee!
const express = require('express'),
mongoose = require('mongoose'),
morgan = require('morgan'),
bodyParser = require('body-parser');
const app = express();
// -- import our app stuff
const api = require('./app/api');
// -- get the config
const config = require('./config');
// -- connect to the db
mongoose.connect(config.mongoUrl);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => console.log(`Connected to mongo database at ${config.mongoUrl}`));
// -- register routes and middleware
app.use(morgan('short'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use('/api', api);
app.use(function (err, req, res, next) {
if (err.name === 'UnauthorizedError') { // deal with jwt auth errors
res.status(401).json({'error': 'auth', 'message': err.message});
}
});
// -- start stuff up
app.listen(config.port, config.host);
console.log(`Server listening on port ${config.port}`);