-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
37 lines (28 loc) · 1.24 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
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const app = express();
require('dotenv').config();
require('./config/database');
app.use(logger('dev'));
app.use(express.json());
// Configure both serve-favicon & static middlewares to serve from the production 'build' folder
app.use(favicon(path.join(__dirname, 'build', 'favicon.ico')));
app.use(express.static(path.join(__dirname, 'build')));
// Put API routes here, before the "catch all" route
app.use('/api/users', require('./routes/api/users'));
app.use(require('./config/auth')); //Attaches user to req.user
app.use('/api/habitGenerator', require('./routes/api/habitGenerator'));
app.use('/api', function (req, res) {
res.status(404).json({ error: 'Resource not found' });
})
// "catch all" route (note the *)is necessary for a SPA's client-side routing to properly work
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}`)
});