-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
175 lines (154 loc) · 7.61 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
var express = require('express'),
mongoose = require("mongoose"),
flash = require("connect-flash"),
bodyParser = require("body-parser"),
methodOverride = require("method-override"),
cookieParser = require("cookie-parser"),
expressSession = require("express-session"),
passport = require('passport'),
LinkedinStrategy = require('passport-linkedin-oauth2').Strategy, //this package is not yet compatible with LinkedIn OAuth v2
// LinkedinStrategy = require('@sokratis/passport-linkedin-oauth2').Strategy,
//seedDB = require("./seed"),
expressSanitizer = require("express-sanitizer"),
//forceSsl = require('force-ssl-heroku'),
app = express();
// **********************
// Hookup Routes
// **********************
var appIndex = require("./routes/app/index"),
appAuth = require("./routes/app/auth"),
appUsers = require("./routes/app/user/user"),
appCoverLetter = require("./routes/app/user/cover-letter"),
appReferences = require("./routes/app/user/reference"),
appResumeIndex = require("./routes/app/build-resume/index"),
// appBundle : require("./routes/app/user/bundle")
apiResumeIndex = require("./routes/api/build-resume/index"),
apiTimeline = require("./routes/api/build-resume/timeline"),
apiSkills = require("./routes/api/build-resume/skills"),
apiExperience = require("./routes/api/build-resume/experience"),
apiEducation = require("./routes/api/build-resume/education"),
apiInterests = require("./routes/api/build-resume/interests"),
apiProjects = require("./routes/api/build-resume/projects"),
apiQuotes = require("./routes/api/build-resume/quotes"),
apiOther = require("./routes/api/build-resume/other"),
apiUsers = require("./routes/api/user/user"),
apiCoverLetter = require("./routes/api/user/cover-letter"),
apiReferences = require("./routes/api/user/reference"),
apiData = require("./routes/api/data/index"),
apiBundle = require("./routes/api/user/bundle");
// creating an array of our routes, so we dont have to individually call app.use() to all of them (ln ~158)
var appRoutes = [appAuth, appUsers, appIndex, appCoverLetter, appReferences, appResumeIndex];
var apiRoutes = [apiResumeIndex, apiTimeline, apiSkills, apiExperience, apiEducation, apiInterests, apiProjects, apiQuotes, apiOther, apiUsers, apiCoverLetter, apiReferences, apiData, apiBundle];
// **********************
// Database Config
// Using environment variables here to distinguish between our test (Local C9) db version, and our "prod" or "deployed" (MLab) db version
// In order to do so, we need to run the following commands to CREATE an environment variable in both enviornments
// 1) For Cloud9, run cmd: export DATABASEURL=mongodb://localhost/yelp_camp
// 2) For Heroku, run cmd: heroku config:set DATABASEURL=mongodb://<username>:<password>@ds219318.mlab.com:19318/yelpcamp
// OR: you can go to your Heroku account, and under settings of your app find "config vars" and manually add key: DATABASEURL value: { heroku url string }
// URL for this Heroku db: mongodb://<username>:<password>@ds219318.mlab.com:19318/yelpcamp
// **********************
console.log(process.env.DATABASECON || "mongodb://localhost/MyResume_v3");
mongoose.connect(process.env.DATABASECON || "mongodb://localhost/MyResume_v3");
// **********************
// Various custom config
// **********************
// Needs to come before passport config
app.use(flash()); //used for our flash messages... this lib is pretty old so hold on
app.locals.moment = require('moment'); //MomentJS
// **********************
// Seteup Passport and LinkedIn-OAuth2
// **********************
// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session. Typically,
// this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing. However, since this example does not
// have a database of user records, the complete Linkedin profile is
// serialized and deserialized.
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
// API Access link for creating client ID and secret:
// https://www.linkedin.com/secure/developer
var LINKEDIN_CLIENT_ID = process.env.LINKEDIN_CLIENT_ID;
var LINKEDIN_CLIENT_SECRET = process.env.LINKEDIN_CLIENT_SECRET;
var LINKEDIN_CALLBACK = process.env.LINKEDIN_CALLBACK;
// Use the LinkedinStrategy within Passport.
// Strategies in Passport require a `verify` function, which accept
// credentials (in this case, an accessToken, refreshToken, and Linkedin
// profile), and invoke a callback with a user object.
passport.use(new LinkedinStrategy({
clientID: LINKEDIN_CLIENT_ID,
clientSecret: LINKEDIN_CLIENT_SECRET,
callbackURL: LINKEDIN_CALLBACK || "https://scroll-with-me-conklin20.c9users.io/auth/linkedin/callback",
scope: ['r_liteprofile', 'r_emailaddress'],
// scope: ['r_basicprofile'],
// profileFields: [
// "formatted-name",
// "headline",
// "id",
// "public-profile-url",
// "email-address",
// "location",
// ],
passReqToCallback: true
},
function(req, accessToken, refreshToken, profile, done) {
// asynchronous verification, for effect...
req.session.accessToken = accessToken;
process.nextTick(function() {
return done(null, profile);
});
}
));
// **********************
// Configure Express
// **********************
app.set('views', [__dirname + '/views',
__dirname + '/views/build-resume',
__dirname + '/views/user',
__dirname + '/views/cover-letter',
__dirname + '/views/reference',
__dirname + '/views/prints']);
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressSanitizer()); //MUST GO AFTER BODY PARSER
app.use(cookieParser());
app.use(express.json());
app.use(expressSession({ secret: 'keyboard cat', resave: true, saveUninitialized: true }));
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(__dirname + '/public'));
app.use(methodOverride("_method")); //overriding HTML froms ability to only send POST and GET routes
// **********************
// Custom middleware
// **********************
// global vars
app.use(function(req, res, next) {
res.locals.currentUser = req.user;
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
res.locals.warning = req.flash("warning");
// res.locals.testing = DEV_ENV;
res.locals.impersonate = false;
next();
});
// **********************
// Use our Routes
// **********************
app.use(appRoutes);
app.use(apiRoutes);
//seed our db for testing only
//seedDB();
// **********************
// Start the server
// **********************
app.listen(process.env.PORT, process.env.IP, function() {
console.log("Server started...");
});