390 lines
14 KiB
HTML
390 lines
14 KiB
HTML
<!DOCTYPE html>
|
|
|
|
<html>
|
|
<head>
|
|
<title>server.js</title>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
|
|
<link rel="stylesheet" media="all" href="docco.css" />
|
|
</head>
|
|
<body>
|
|
<div id="container">
|
|
<div id="background"></div>
|
|
|
|
<ul id="jump_to">
|
|
<li>
|
|
<a class="large" href="javascript:void(0);">Jump To …</a>
|
|
<a class="small" href="javascript:void(0);">+</a>
|
|
<div id="jump_wrapper">
|
|
<div id="jump_page">
|
|
|
|
|
|
<a class="source" href="config.html">
|
|
config.js
|
|
</a>
|
|
|
|
|
|
<a class="source" href="middleware.html">
|
|
middleware.js
|
|
</a>
|
|
|
|
|
|
<a class="source" href="seed.html">
|
|
seed.js
|
|
</a>
|
|
|
|
|
|
<a class="source" href="server.html">
|
|
server.js
|
|
</a>
|
|
|
|
|
|
<a class="source" href="utils.html">
|
|
utils.js
|
|
</a>
|
|
|
|
</div>
|
|
</li>
|
|
</ul>
|
|
|
|
<ul class="sections">
|
|
|
|
<li id="title">
|
|
<div class="annotation">
|
|
<h1>server.js</h1>
|
|
</div>
|
|
</li>
|
|
|
|
|
|
|
|
<li id="section-1">
|
|
<div class="annotation">
|
|
|
|
<div class="pilwrap ">
|
|
<a class="pilcrow" href="#section-1">¶</a>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="content"><div class='highlight'><pre>require(<span class="string">'coffee-script'</span>) <span class="comment">// remove this once we've fully converted over</span>
|
|
|
|
<span class="keyword">var</span> express = require(<span class="string">"express"</span>);
|
|
<span class="keyword">var</span> http = require(<span class="string">"http"</span>);
|
|
<span class="keyword">var</span> path = require(<span class="string">"path"</span>);
|
|
<span class="keyword">var</span> app = express();
|
|
<span class="keyword">var</span> nconf = require(<span class="string">'nconf'</span>);
|
|
<span class="keyword">var</span> utils = require(<span class="string">'./utils'</span>);
|
|
<span class="keyword">var</span> middleware = require(<span class="string">'./middleware'</span>);
|
|
<span class="keyword">var</span> server;
|
|
<span class="keyword">var</span> TWO_WEEKS = <span class="number">1000</span> * <span class="number">60</span> * <span class="number">60</span> * <span class="number">24</span> * <span class="number">14</span>;</pre></div></div>
|
|
|
|
</li>
|
|
|
|
|
|
<li id="section-2">
|
|
<div class="annotation">
|
|
|
|
<div class="pilwrap ">
|
|
<a class="pilcrow" href="#section-2">¶</a>
|
|
</div>
|
|
<p>------------ Setup configurations ------------</p>
|
|
|
|
</div>
|
|
|
|
<div class="content"><div class='highlight'><pre>require(<span class="string">'./config'</span>);
|
|
process.on(<span class="string">"uncaughtException"</span>, <span class="keyword">function</span>(error) {</pre></div></div>
|
|
|
|
</li>
|
|
|
|
|
|
<li id="section-3">
|
|
<div class="annotation">
|
|
|
|
<div class="pilwrap ">
|
|
<a class="pilcrow" href="#section-3">¶</a>
|
|
</div>
|
|
<p>when we hit an error, send it to admin as an email. If no ADMIN_EMAIL is present, just send it to yourself (SMTP_USER)</p>
|
|
|
|
</div>
|
|
|
|
<div class="content"><div class='highlight'><pre> utils.sendEmail({
|
|
from: <span class="string">"HabitRPG <"</span> + nconf.get(<span class="string">'SMTP_USER'</span>) + <span class="string">">"</span>,
|
|
to: nconf.get(<span class="string">'ADMIN_EMAIL'</span>) || nconf.get(<span class="string">'SMTP_USER'</span>),
|
|
subject: <span class="string">"HabitRPG Error"</span>,
|
|
text: error.stack
|
|
});
|
|
console.error(error.stack);
|
|
});</pre></div></div>
|
|
|
|
</li>
|
|
|
|
|
|
<li id="section-4">
|
|
<div class="annotation">
|
|
|
|
<div class="pilwrap ">
|
|
<a class="pilcrow" href="#section-4">¶</a>
|
|
</div>
|
|
<p>------------ MongoDB Configuration ------------</p>
|
|
|
|
</div>
|
|
|
|
<div class="content"><div class='highlight'><pre>mongoose = require(<span class="string">'mongoose'</span>);
|
|
require(<span class="string">'./models/user'</span>); <span class="comment">//load up the user schema - TODO is this necessary?</span>
|
|
require(<span class="string">'./models/group'</span>);
|
|
mongoose.connect(nconf.get(<span class="string">'NODE_DB_URI'</span>), <span class="keyword">function</span>(err) {
|
|
<span class="keyword">if</span> (err) <span class="keyword">throw</span> err;
|
|
console.info(<span class="string">'Connected with Mongoose'</span>);
|
|
});</pre></div></div>
|
|
|
|
</li>
|
|
|
|
|
|
<li id="section-5">
|
|
<div class="annotation">
|
|
|
|
<div class="pilwrap ">
|
|
<a class="pilcrow" href="#section-5">¶</a>
|
|
</div>
|
|
<p>------------ Passport Configuration ------------</p>
|
|
|
|
</div>
|
|
|
|
<div class="content"><div class='highlight'><pre><span class="keyword">var</span> passport = require(<span class="string">'passport'</span>)
|
|
<span class="keyword">var</span> util = require(<span class="string">'util'</span>)
|
|
<span class="keyword">var</span> FacebookStrategy = require(<span class="string">'passport-facebook'</span>).Strategy;</pre></div></div>
|
|
|
|
</li>
|
|
|
|
|
|
<li id="section-6">
|
|
<div class="annotation">
|
|
|
|
<div class="pilwrap ">
|
|
<a class="pilcrow" href="#section-6">¶</a>
|
|
</div>
|
|
<p>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 Facebook profile is serialized
|
|
and deserialized.</p>
|
|
|
|
</div>
|
|
|
|
<div class="content"><div class='highlight'><pre>passport.serializeUser(<span class="keyword">function</span>(user, done) {
|
|
done(<span class="literal">null</span>, user);
|
|
});
|
|
|
|
passport.deserializeUser(<span class="keyword">function</span>(obj, done) {
|
|
done(<span class="literal">null</span>, obj);
|
|
});</pre></div></div>
|
|
|
|
</li>
|
|
|
|
|
|
<li id="section-7">
|
|
<div class="annotation">
|
|
|
|
<div class="pilwrap ">
|
|
<a class="pilcrow" href="#section-7">¶</a>
|
|
</div>
|
|
<p>Use the FacebookStrategy within Passport.
|
|
Strategies in Passport require a <code>verify</code> function, which accept
|
|
credentials (in this case, an accessToken, refreshToken, and Facebook
|
|
profile), and invoke a callback with a user object.</p>
|
|
|
|
</div>
|
|
|
|
<div class="content"><div class='highlight'><pre>passport.use(<span class="keyword">new</span> FacebookStrategy({
|
|
clientID: nconf.get(<span class="string">"FACEBOOK_KEY"</span>),
|
|
clientSecret: nconf.get(<span class="string">"FACEBOOK_SECRET"</span>),
|
|
callbackURL: nconf.get(<span class="string">"BASE_URL"</span>) + <span class="string">"/auth/facebook/callback"</span>
|
|
},
|
|
<span class="keyword">function</span>(accessToken, refreshToken, profile, done) {</pre></div></div>
|
|
|
|
</li>
|
|
|
|
|
|
<li id="section-8">
|
|
<div class="annotation">
|
|
|
|
<div class="pilwrap ">
|
|
<a class="pilcrow" href="#section-8">¶</a>
|
|
</div>
|
|
<p>asynchronous verification, for effect...
|
|
process.nextTick(function () {</p>
|
|
<p>To keep the example simple, the user's Facebook profile is returned to
|
|
represent the logged-in user. In a typical application, you would want
|
|
to associate the Facebook account with a user record in your database,
|
|
and return that user instead.</p>
|
|
|
|
</div>
|
|
|
|
<div class="content"><div class='highlight'><pre> <span class="keyword">return</span> done(<span class="literal">null</span>, profile);</pre></div></div>
|
|
|
|
</li>
|
|
|
|
|
|
<li id="section-9">
|
|
<div class="annotation">
|
|
|
|
<div class="pilwrap ">
|
|
<a class="pilcrow" href="#section-9">¶</a>
|
|
</div>
|
|
<p>});</p>
|
|
|
|
</div>
|
|
|
|
<div class="content"><div class='highlight'><pre> }
|
|
));</pre></div></div>
|
|
|
|
</li>
|
|
|
|
|
|
<li id="section-10">
|
|
<div class="annotation">
|
|
|
|
<div class="pilwrap ">
|
|
<a class="pilcrow" href="#section-10">¶</a>
|
|
</div>
|
|
<p>------------ Server Configuration ------------</p>
|
|
|
|
</div>
|
|
|
|
<div class="content"><div class='highlight'><pre>app.set(<span class="string">"port"</span>, nconf.get(<span class="string">'PORT'</span>));
|
|
app.use(express.logger(<span class="string">"dev"</span>));
|
|
app.use(express.compress());
|
|
app.set(<span class="string">"views"</span>, __dirname + <span class="string">"/../views"</span>);
|
|
app.set(<span class="string">"view engine"</span>, <span class="string">"jade"</span>);
|
|
app.use(express.favicon());
|
|
app.use(middleware.cors);
|
|
app.use(middleware.forceSSL);
|
|
app.use(express.bodyParser());
|
|
app.use(express.methodOverride());</pre></div></div>
|
|
|
|
</li>
|
|
|
|
|
|
<li id="section-11">
|
|
<div class="annotation">
|
|
|
|
<div class="pilwrap ">
|
|
<a class="pilcrow" href="#section-11">¶</a>
|
|
</div>
|
|
<p>app.use(express.cookieParser(nconf.get('SESSION_SECRET')));</p>
|
|
|
|
</div>
|
|
|
|
<div class="content"><div class='highlight'><pre>app.use(express.cookieParser());
|
|
app.use(express.cookieSession({ secret: nconf.get(<span class="string">'SESSION_SECRET'</span>), httpOnly: <span class="literal">false</span>, cookie: { maxAge: TWO_WEEKS }}));</pre></div></div>
|
|
|
|
</li>
|
|
|
|
|
|
<li id="section-12">
|
|
<div class="annotation">
|
|
|
|
<div class="pilwrap ">
|
|
<a class="pilcrow" href="#section-12">¶</a>
|
|
</div>
|
|
<p>app.use(express.session());</p>
|
|
|
|
</div>
|
|
|
|
<div class="content"><div class='highlight'><pre>app.use(middleware.splash);
|
|
app.use(middleware.locals);</pre></div></div>
|
|
|
|
</li>
|
|
|
|
|
|
<li id="section-13">
|
|
<div class="annotation">
|
|
|
|
<div class="pilwrap ">
|
|
<a class="pilcrow" href="#section-13">¶</a>
|
|
</div>
|
|
<p>Initialize Passport! Also use passport.session() middleware, to support
|
|
persistent login sessions (recommended).</p>
|
|
|
|
</div>
|
|
|
|
<div class="content"><div class='highlight'><pre>app.use(passport.initialize());
|
|
app.use(passport.session());
|
|
|
|
app.use(app.router);
|
|
|
|
<span class="keyword">var</span> oneYear = <span class="number">31536000000</span>;
|
|
app.use(express[<span class="string">'static'</span>](path.join(__dirname, <span class="string">"/../build"</span>), { maxAge: oneYear }));
|
|
app.use(express[<span class="string">'static'</span>](path.join(__dirname, <span class="string">"/../public"</span>)));</pre></div></div>
|
|
|
|
</li>
|
|
|
|
|
|
<li id="section-14">
|
|
<div class="annotation">
|
|
|
|
<div class="pilwrap ">
|
|
<a class="pilcrow" href="#section-14">¶</a>
|
|
</div>
|
|
<p>development only</p>
|
|
|
|
</div>
|
|
|
|
<div class="content"><div class='highlight'><pre><span class="keyword">if</span> (<span class="string">"development"</span> === app.get(<span class="string">"env"</span>)) {
|
|
app.use(express.errorHandler());
|
|
}</pre></div></div>
|
|
|
|
</li>
|
|
|
|
|
|
<li id="section-15">
|
|
<div class="annotation">
|
|
|
|
<div class="pilwrap ">
|
|
<a class="pilcrow" href="#section-15">¶</a>
|
|
</div>
|
|
<p>Custom Directives</p>
|
|
|
|
</div>
|
|
|
|
<div class="content"><div class='highlight'><pre>app.use(require(<span class="string">'./routes/pages'</span>).middleware);
|
|
app.use(require(<span class="string">'./routes/auth'</span>).middleware);
|
|
app.use(<span class="string">'/api/v1'</span>, require(<span class="string">'./routes/api'</span>).middleware);
|
|
app.use(require(<span class="string">'./controllers/deprecated'</span>).middleware);
|
|
server = http.createServer(app).listen(app.get(<span class="string">"port"</span>), <span class="keyword">function</span>() {
|
|
<span class="keyword">return</span> console.log(<span class="string">"Express server listening on port "</span> + app.get(<span class="string">"port"</span>));
|
|
});
|
|
|
|
module.exports = server;
|
|
|
|
<span class="comment">/*
|
|
#ONE_YEAR = 1000 * 60 * 60 * 24 * 365
|
|
#root = path.dirname path.dirname __dirname
|
|
#publicPath = path.join root, 'public'
|
|
#
|
|
#
|
|
#expressApp
|
|
# .use(express.favicon("#{publicPath}/favicon.ico"))
|
|
# # Gzip static files and serve from memory
|
|
# .use(gzippo.staticGzip(publicPath, maxAge: ONE_YEAR))
|
|
# # Gzip dynamically rendered content
|
|
# .use(express.compress())
|
|
# .use(middleware.translate)
|
|
# .use(auth.middleware(strategies, options))
|
|
# .use(serverError(root))
|
|
#
|
|
#
|
|
## Errors
|
|
#expressApp.all '*', (req) ->
|
|
# throw "404: #{req.url}"
|
|
*/</span></pre></div></div>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|