-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
76 lines (60 loc) · 2.39 KB
/
Copy pathserver.js
File metadata and controls
76 lines (60 loc) · 2.39 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
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
//dependencies
import sqreen from 'sqreen';
import path from 'path';
import express from 'express';
import mongoose from 'mongoose';
import morgan from 'morgan';
import bodyParser from 'body-parser';
import config from './config/config.js';
import usersRouter from './routes/usersRouter.js';
import adminRouter from './routes/adminRouter.js';
import chargesRouter from './routes/chargesRouter.js';
import imageRouter from './routes/imageRouter.js';
import forumRouter from './routes/forumRouter.js';
import cors from 'cors';
import Sentry from '@sentry/node';
Sentry.init({ dsn: 'https://6b0b414cfb644321982e20ed8319e634@o375820.ingest.sentry.io/5195824' });
// The request handler must be the first middleware on the app
//connect to database
mongoose.connect(config.db.uri, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false }).then(() => {
console.log(`Successfully connected to mongoose database.`)
});
//initialize app
const app = express();
app.use(Sentry.Handlers.requestHandler());
//enable request logging for development debugging
app.use(morgan('dev'));
//body parsing middleware
app.use(bodyParser.urlencoded({
extended: true
}));
//parses json files
app.use(bodyParser.json());
/* serve static files - see http://expressjs.com/en/starter/static-files.html */
app.use('/', express.static('./client/build'));
app.use(express.static('./client/build'))
//https://enable-cors.org/server_expressjs.html
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:3001"); // update to match the domain you will make the request from
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(cors());
app.use('/api/users/', usersRouter);
app.use('/api/admin/', adminRouter);
app.use('/api/stripe/', chargesRouter);
app.use('/api/image/', imageRouter);
app.use('/api/forum/', forumRouter);
app.use(Sentry.Handlers.errorHandler());
app.all('/*', (req, res) => {
// res.status(201).json({message: "nothing here!"});
res.sendFile(path.resolve("./client/build/index.html"));
});
app.use(function onError(err, req, res, next) {
// The error id is attached to `res.sentry` to be returned
// and optionally displayed to the user for support.
res.statusCode = 500;
res.end(res.sentry + "\n");
});
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => console.log(`App now listening on port ${PORT}`));