-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
113 lines (90 loc) · 3.77 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';
import express from 'express';
import mongoose from 'mongoose';
import bodyParser from 'body-parser';
import morgan from 'morgan';
import apiRouter from './api';
import authRouter from './auth';
import fs from 'fs';
import Bird from './models/bird';
// import environmental variables from our variables.env file
require('dotenv').config({ path: 'variables.env' });
const path = require('path');
const app = express();
// console.log(process.env.NODE_ENV);
//set our port to either a predetermined port number if you have set it up, or 3001
const port = process.env.API_PORT || 3001;
// Connect to our Database
mongoose.connect(process.env.DATABASE);
// converts raw requests into usaxsble properties on req.body
// This object will contain key-value pairs, where the value can be a string or array (when extended is false), or any type (when extended is true).
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false}));
app.use(morgan('dev'));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
next();
});
app.use('/api', apiRouter);
app.use('/api/auth', authRouter);
// app.get('/', function(request, response) {
// const filePath = path.resolve(__dirname, './build', 'index.html');
// // read in the index.html file
// fs.readFile(filePath, 'utf8', function (err,data) {
// if (err) {
// return console.log(err);
// }
// // replace the special strings with server generated strings
// data = data.replace(/\$OG_TITLE/g, 'BirdNerd App');
// data = data.replace(/\$OG_DESCRIPTION/g, "BirdNerd is a free photo sharing site for Birdwatchers. Within this site you can view photos, sighting locations and general bird information.");
// let result = data.replace(/\$OG_IMAGE/g, 'https://cdn.dribbble.com/users/224707/screenshots/1966613/birdnerd.jpg');
// response.send(result);
// });
// });
// app.get('/bird/:birdSlug', function(request, response) {
// const birdSlug = request.params.birdSlug;
// const filePath = path.resolve(__dirname, './build', 'index.html')
// fs.readFile(filePath, 'utf8', function (err,template) {
// if (err) {
// return console.log(err);
// }
// template = template.replace(/\$OG_TITLE/g, `Bird: ${birdSlug}`);
// Bird.findOne( { slug: birdSlug } )
// .exec()
// .then(data => {
// template = template.replace(/\$OG_DESCRIPTION/g, `${data.comments}`);
// let result = template.replace(/\$OG_IMAGE/g, `${data.imageUrl}`);
// response.send(result);
// })
// .catch(err => {
// console.log(err);
// response.json(err);
// });
// });
// });
// app.get('/contact', function(request, response) {
// console.log('Contact page visited!');
// const filePath = path.resolve(__dirname, './build', 'index.html')
// fs.readFile(filePath, 'utf8', function (err,data) {
// if (err) {
// return console.log(err);
// }
// data = data.replace(/\$OG_TITLE/g, 'Contact Page');
// data = data.replace(/\$OG_DESCRIPTION/g, "Contact page description");
// result = data.replace(/\$OG_IMAGE/g, 'https://i.imgur.com/V7irMl8.png');
// response.send(result);
// });
// });
// Express only serves static assets in production
if (process.env.NODE_ENV === 'production') {
app.use(express.static('build'));
}
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'build', 'index.html'));
});
//starts the server and listens for requests
app.listen(port, function() {
console.log(`api running on port ${port} in ${process.env.NODE_ENV}`);
});