-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
53 lines (39 loc) · 1.43 KB
/
app.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
const error = require('./middleware/error');
const express = require('express');
const mongoose = require('mongoose');
const logger = require('morgan');
const bodyParser = require('body-parser');
const config = require('config');
const helmet = require('helmet');
const compression = require('compression');
const app = express();
// varible to save api routes word routes
const words = require('./routes/api/words');
const users = require('./routes/api/users');
const auth = require('./routes/api/auth');
if (!config.get('jwtPrivateKey')){
console.log('FATAL ERROR: jwtPrivateKey is not defined');
process.exit(1);
}
// mongoDB Setup and Connection
const dbRoute = 'mongodb+srv://yaumil:[email protected]/basa-db?retryWrites=true';
mongoose.connect( dbRoute, { useNewUrlParser: true } );
let db = mongoose.connection;
db.once('open', () => console.log('Connected to MongoDB'));
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.json());
app.use(logger('dev'));
app.use(helmet());
app.use(compression());
// pass route file for the specific url
app.use('/api/words', words);
app.use('/api/users', users);
app.use('/api/auth', auth);
app.use(error);
// set port and launch node into a port
const port = process.env.PORT || 3001;
app.listen(port, () => {
console.log(`listening on port ${port}...`);
});