/
server.js
61 lines (48 loc) · 1.81 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const express = require('express');
const morgan = require('morgan');
const mongoose = require('mongoose');
const path = require('path');
const routes = require('./routes/api');
const { MongoClient } = require('mongodb');
const http = require('http');
const app = express();
const PORT = process.env.PORT; //|| 8000;
//hanna kcb8cXbzGAEzdGlJ
//mongodb+srv://hanna:<kcb8cXbzGAEzdGlJ>@useraccounts.bx3zc.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
//heroku => comp426finalhannalt
//const url = "mongodb+srv://hanna:kcb8cXbzGAEzdGlJ@useraccounts.bx3zc.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
//const client = new MongoClient(url, { useUnifiedTopology: true });
//const sockets = require('./routes/sockets');
mongoose.connect(process.env.MONGODB_URI , {
useNewUrlParser: true,
useUnifiedTopology: true
}) //|| 'mongodb://localhost/mern_youtube'
mongoose.connection.on('connected', () => {
console.log('Mongoose is connected')
})
app.use(express.json());
app.use(express.urlencoded({ extended: false }))
/*async function run() {
try {
await client.connect();
console.log("Connected correctly to server");
} catch (err) {
console.log(err.stack);
}
finally {
await client.close();
}
}
run().catch(console.dir);*/
app.use(morgan('tiny'));
app.use('/api', routes);
//if (process.env.NODE_ENV === "production") {
// Serve static files from the React frontend app
app.use(express.static(path.join(__dirname, './client/build')))
console.log('here')
// AFTER defining routes: Anything that doesn't match what's above, send back index.html; (the beginning slash ('/') in the string is important!)
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + './client/build/index.html'))
})
//}
app.listen(PORT, console.log(`Server is running on ${PORT}`))