-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
48 lines (37 loc) · 1.56 KB
/
Copy pathserver.js
File metadata and controls
48 lines (37 loc) · 1.56 KB
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
require('dotenv').config()
// Connect to the database
require('./config/database');
const express = require('express');
const path = require('path');
// const favicon = require('serve-favicon');
// favicon is NEW - for server routing
const logger = require('morgan');
const checkToken = require('./config/checkToken')
const app = express();
app.use(logger('dev'));
app.use(express.json());
// app.use(favicon(path.join(__dirname, 'build', 'favicon.ico')));
// Configure both serve-favicon & static middleware to serve from the production 'build' folder
app.use(express.static(path.join(__dirname, 'build')));
app.use(checkToken)
//! Put API routes here, before the "catch all" route
app.use('/api/courses', require('./routes/api/courses'))
app.use('/api/users', require('./routes/api/users'))
// ----------------- DEPLOYMENT -------------------
if (process.env.NODE_ENV === 'production'){
app.use(express.static('oncourse/build"'))
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, 'build', 'index.html' ))
})
}
// ----------------- DEPLOYMENT -------------------
// The following "catch all" route (note the *) is necessary to return the index.html on all non-AJAX requests
//? https://create-react-app.dev/docs/deployment/
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
//! Configure to use port 3001 instead of 3000 during development to avoid collision with React's dev server
const port = process.env.PORT || 3001
app.listen(port, function() {
console.log(`Express app running on port ${port}`)
});