forked from mozilla/MakeAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·63 lines (55 loc) · 2.4 KB
/
server.js
File metadata and controls
executable file
·63 lines (55 loc) · 2.4 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// New Relic Server monitoring support
if ( process.env.NEW_RELIC_ENABLED ) {
require( "newrelic" );
}
// Bring in all your require modules
var express = require( "express" ),
habitat = require( "habitat" ),
nunjucks = require( "nunjucks" ),
path = require( "path" );
// Load config from ".env"
habitat.load();
// Generate app variables
var app = express(),
env = new habitat(),
Mongo = require( "./lib/mongoose" )( env ),
Make = require( "./lib/models/make" )( env, Mongo.mongoInstance() ),
middleware = require( "./lib/middleware" )( env ),
nunjucksEnv = new nunjucks.Environment( new nunjucks.FileSystemLoader( path.join( __dirname + "/views" ) ) ),
routes = require( "./routes" )( Make, env );
// Enable template rendering with nunjucks
nunjucksEnv.express( app );
// Don't send the "X-Powered-By: Express" header
app.disable( "x-powered-by" );
app.use( express.logger( env.get( "NODE_ENV" ) === "production" ? "" : "dev" ) );
app.use( express.compress() );
app.use( express.static( path.join( __dirname + "/public" ) ) );
app.use( express.bodyParser() );
app.use( express.cookieParser() );
app.use( express.cookieSession({
secret: env.get( "SESSION_SECRET" ),
cookie: {
maxAge: 2678400000 // 31 days. Persona saves session data for 1 month
},
proxy: true
}));
app.get( "/", routes.index );
app.post( "/api/make", express.basicAuth( middleware.authenticateUser ), Mongo.isDbOnline, routes.create );
app.put( "/api/make/:id", express.basicAuth( middleware.authenticateUser ), Mongo.isDbOnline, routes.update );
app.del( "/api/make/:id", express.basicAuth( middleware.authenticateUser ), Mongo.isDbOnline, routes.remove );
app.get( "/api/makes/search", Mongo.isDbOnline, function crossOrigin( req, res, next ) {
res.header( "Access-Control-Allow-Origin", "*" );
next();
}, routes.search );
app.options( "/api/makes/search", function( req, res ) {
res.header( "Access-Control-Allow-Origin", "*" );
res.header( "Access-Control-Allow-Headers", "Content-Type" );
res.send( 200 );
});
app.get( "/healthcheck", routes.healthcheck );
app.listen( env.get( "PORT", 3000 ), function() {
console.log( "MakeAPI server listening ( Probably http://localhost:%d )", env.get( "PORT", 3000 ));
});