-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
67 lines (57 loc) · 2.23 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
62
63
64
65
66
67
const express = require('express');
const path = require('path');
const config = require('config');
const mongoose = require('mongoose');
require('dotenv').config()
let app = express();
const dbURI = `mongodb+srv://${process.env.MONGO_DB_USERNAME}:${process.env.MONGO_DB_PASSWORD}@${process.env.MONGO_DB_CLUSTER}/timelessDatabase`
// const dbURI = config.get('dbURI'); //This does the same as above but with less process.env
// Initialising our routes
const authRoutes = require('./routes/auth');
const itemRoutes = require('./routes/item');
const cartRoutes = require('./routes/cart');
const orderRoutes = require('./routes/order');
// For body parser stuff
app.use(express.urlencoded({extended:true}));
app.use(express.json());
// If production NODE_ENV becomes production then build the production build in this location then fetch everything from that build and send it as a response here.
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, 'timeless-app-frontend/build')));
app.get('*',(req,res) =>
{res.sendFile(path.resolve(__dirname, 'timeless-app-frontend', 'build', 'index.html'));
});
}
// Setting up our route paths to /api/ route.
app.use('/api',authRoutes);
app.use('/api',itemRoutes);
app.use('/api',cartRoutes);
app.use('/api',orderRoutes);
// Initialising Mongoose.
mongoose.Promise = global.Promise;
// Connecting to the database
mongoose.connect(dbURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
})
// Error messages
mongoose.connection.on('error', function(err) {
console.log('Connection to Mongo established.');
console.log('Could not connect to the database. Exiting now...');
process.exit();
});
// If link has been established message
mongoose.connection.once('open', function() {
console.log("Successfully connected to the database");
})
// If server has a 404 error display this message.
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// Listening on process.env.PORT for HEROKU deoplyment else it will listen to 8000 locally.
app.listen(process.env.PORT || 8000, () => {
console.log("Listening on port " + (process.env.PORT || 8000))
})
module.exports = app;