-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (35 loc) · 1.28 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
const express = require('express');
const app = express();
const dest = `${__dirname}/public`;
const mongoose = require('mongoose');
const bluebird = require('bluebird');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const cors = require('cors');
// const chai = require('chai');
const expressJWT = require('express-jwt');
const config = require('./config/config');
const routes = require('./config/routes');
mongoose.connect(config.db);
mongoose.Promise = bluebird;
if (app.get('env') !== 'production') app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({encoded: true}));
app.use(morgan('dev'));
app.use(express.static(dest));
app.use('/api', expressJWT({secret: config.secret})
.unless({
path: [
{url: '/api/register', methods: ['POST']},
{url: '/api/login', methods: ['POST']}
]
}));
app.use(jwtErrorHandler);
function jwtErrorHandler(err, req, res, next){
if(err.name !== 'UnauthorizedError') return next();
return res.status(401).json({ message: 'Unauthorized request.'});
}
app.use('/api', routes);
app.get('/*', (req, res) => res.sendFile(`${dest}/index.html`));
app.listen(config.port, () => console.log(`Express has started on port: ${config.port}`));
module.exports = app;