-
Notifications
You must be signed in to change notification settings - Fork 69
/
server.js
40 lines (27 loc) · 861 Bytes
/
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
38
39
40
// Import npm packages
const express = require('express');
const mongoose = require('mongoose');
const morgan = require('morgan');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 8080; // Step 1
const routes = require('./routes/api');
// Step 2
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost/mern_youtube', {
useNewUrlParser: true,
useUnifiedTopology: true
});
mongoose.connection.on('connected', () => {
console.log('Mongoose is connected!!!!');
});
// Data parsing
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// Step 3
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
// HTTP request logger
app.use(morgan('tiny'));
app.use('/api', routes);
app.listen(PORT, console.log(`Server is starting at ${PORT}`));