Skip to content

Instantly share code, notes, and snippets.

@obrientimothya
Created January 25, 2016 04:09
Show Gist options
  • Save obrientimothya/20f0adb8c06ca2e28ef5 to your computer and use it in GitHub Desktop.
Save obrientimothya/20f0adb8c06ca2e28ef5 to your computer and use it in GitHub Desktop.
api-jwt /app.js
// 1. Include Packages
var express = require("express");
var bodyParser = require('body-parser');
var mongoose = require("mongoose");
var cors = require("cors");
var logger = require('morgan');
// 2. Include Configuration
var config = require('./config');
// 3. Initialize the application
var app = express();
app.use(cors());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// 4. Force https in production
if (app.get('env') === 'production') {
app.use(function(req, res, next) {
var protocol = req.get('x-forwarded-proto');
protocol == 'https' ? next() : res.redirect('https://' + req.hostname + req.url);
});
}
// 5. Connect to MongoDB
mongoose.connect(config.MONGO_URI);
mongoose.connection.on('error', function(err) {
console.log('Error: Could not connect to MongoDB.');
});
// 6. Load app routes
require('./routes')(app);
// 7. Start the server
app.listen(config.LISTEN_PORT, function(){
console.log('listening on port ' + config.LISTEN_PORT);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment