Created
November 15, 2014 03:31
-
-
Save ryankurte/52b3b9780a23c340ea1c to your computer and use it in GitHub Desktop.
Node.js Express Starter App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
/*** Application Setup ***/ | |
//Create express app | |
var app = express(); | |
// Set up body parser for retrieval of POST data | |
app.use(bodyParser.urlencoded({ extended: true })); | |
app.use(bodyParser.json()); | |
// Load application settings | |
var port = process.env.PORT || 8000; | |
/*** API bindings ***/ | |
// Create a router for API calls | |
var router = express.Router(); | |
// Middleware to attach generic processing to all calls | |
router.use(function(req, res, next) { | |
//TODO: attach logging here | |
//TODO: attach security here | |
console.log("API Request"); | |
next(); | |
}); | |
// API test route | |
router.get('/', function(req, res) { | |
res.json({ message: 'API ok' }); | |
}); | |
/*** Application Logic ***/ | |
// Bind exit event to finalize database | |
process.on('exit', function(code) { | |
console.log("Exiting server"); | |
db.close(); | |
}); | |
// Bind API router to /api path | |
app.use('/api', router); | |
//Bind static folder | |
app.use(express.static(__dirname + '/static')); | |
// Start the server | |
app.listen(port); | |
console.log("Server running at http://127.0.0.1:" + port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment