-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
66 lines (59 loc) · 2.07 KB
/
app.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
/* setting up express and importing helper node modules*/
const express = require('express');
const path = require('path');
const logger = require('morgan');
const bodyParser = require('body-parser');
const cors = require('cors');
const methodOverride = require('method-override');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const passport = require('passport');
/* dotenv locats the environment variables in the .env file */
require('dotenv').config();
/* creating a variable for the express function*/
const app = express();
/* setting up port to listen to*/
const PORT = process.env.PORT || 3001;
app.listen(PORT, function() {
console.log(`listening on port ${PORT}`);
});
/* setting up cors*/
app.use(cors());
/* setting up logger to log activity when the server is running */
app.use(logger('dev'));
/* setting up body parser to parse the request object*/
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(methodOverride('_method'));
app.use(session({
secret: process.env.SECRET_KEY,
resave: false,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(cookieParser());
/* ###### setting routes ###### */
/* setting up static file redirect*/
app.use(express.static(path.join(__dirname, 'client/build')));
/* index route*/
// app.get('/api', function(req, res) {
// res.sendFile(__dirname + '/public/index.html');
// });
/* importing routes and assigning urls for books api and users api*/
const booksRoutes = require('./routes/booksRoutes');
const usersRoutes = require('./routes/usersRoutes');
const authRoutes = require('./routes/auth');
app.use('/api/books', booksRoutes);
app.use('/api/users', usersRoutes);
app.use('/auth', authRoutes);
app.get('/api/books', booksRoutes);
app.get('/api/users', usersRoutes);
app.get('/auth', authRoutes);
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
/* handling 404*/
app.get('*', function(req,res) {
res.status(404).send({message: 'Oops! Not found.'});
});