-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
85 lines (75 loc) · 2.8 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// setting up variables for node modules
const express = require('express');
const path = require('path');
const logger = require('morgan');
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const passport = require('passport');
// importing dotenv config function
require('dotenv').config();
// creating a variable for the express function
const app = express();
// importing the recipeHelper function for the search view
const recipeHelpers = require('./services/recipes/recipeHelpers');
// importing routes
const recipesRoutes = require('./routes/recipesRoutes');
const authRoutes = require('./routes/auth');
const userRoutes = require('./routes/users')
// setting up port for express to listen to for activity
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`listening on port ${PORT}`);
});
// directing express views
app.set('views', path.join(__dirname, 'views'));
// telling express the view file type
app.set('view engine', 'ejs');
// directing express to static files
app.use('/static', express.static(path.join(__dirname, 'public')));
// setting up the morgan logger function to run on dev script
app.use(logger('dev'));
// setting up the cookie parser module
app.use(cookieParser());
// setting up the body parser function to run on json info
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
// setting up the method override module to run on http method key word
app.use(methodOverride('_method'));
// setting up the express session module
app.use(session({
secret: process.env.SECRET_KEY,
resave: false,
saveUninitialized: true
}));
// setting up the passport module
app.use(passport.initialize());
app.use(passport.session());
// setting up the route to the index page
app.get('/',(req, res) => {
res.render('index', {
documentTitle: 'Now We\'re Cookin\'',
subTitle: 'A collection of our favorite foods and how to make em\'',
message: 'Now We\'re Cookin\'',
});
});
// setting up the route to the search page
app.get('/search', recipeHelpers.getRecipe, (req, res) => {
res.render('search', {
documentTitle: 'Now We\'re Cookin\'',
subTitle: 'Can\'t find what you\'re loking for? Try searching for it here!',
message: 'Powered by',
recipeHits: res.locals.recipeHits,
});
});
// directing the app to use the recipesRoutes for all recipes urls
app.use('/recipes', recipesRoutes);
// directing the app to use the authRoutes for user authentication
app.use('/auth', authRoutes);
// directing the app to use the userRoutes for users
app.use('/user', userRoutes);
// handling 404 errors
app.get('*', (req, res) => {
res.status(404).send({message: 'Spaghetti-oh-no!'});
});