From 845436c34b68f238efb7122b4e627a4e8995ebb4 Mon Sep 17 00:00:00 2001 From: Yemel Jardi Date: Thu, 26 Feb 2015 16:45:40 -0300 Subject: [PATCH] Initial http server structure --- api/app.js | 44 ++++++++++++++++++++++ api/bin/www | 90 +++++++++++++++++++++++++++++++++++++++++++++ api/config.js | 7 ++++ api/routes/index.js | 11 ++++++ package.json | 2 +- 5 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 api/app.js create mode 100755 api/bin/www create mode 100644 api/config.js create mode 100644 api/routes/index.js diff --git a/api/app.js b/api/app.js new file mode 100644 index 00000000..94bc1f3b --- /dev/null +++ b/api/app.js @@ -0,0 +1,44 @@ +'use strict'; + +var express = require('express'); +var bodyParser = require('body-parser'); + +var config = require('./config'); +var routes = require('./routes/index'); + + +function API(backend, opts) { + this.backend = backend; + this.opts = opts; + + this._initApp(); +} + +API.prototype._initApp = function() { + this.app = express(); + + // parse POST data + this.app.use(bodyParser.json()); + this.app.use(bodyParser.urlencoded({ extended: false })); + + // install routes + this.app.use('/v1', routes); + + // catch 404 and forward to error handler + this.app.use(function(req, res, next) { + var err = new Error('Not Found'); + err.status = 404; + next(err); + }); + + // production error handler + this.app.use(function(err, req, res, next) { + res.status(err.status || 500); + res.render('error', { + message: err.message, + error: {} + }); + }); +} + +module.exports = API; diff --git a/api/bin/www b/api/bin/www new file mode 100755 index 00000000..850e5e28 --- /dev/null +++ b/api/bin/www @@ -0,0 +1,90 @@ +#!/usr/bin/env node + +/** + * Module dependencies. + */ + +var API = require('../app'); +var http = require('http'); +var api = new API(); + +/** + * Get port from environment and store in Express. + */ + +var port = normalizePort(process.env.PORT || '8000'); +api.app.set('port', port); + +/** + * Create HTTP server. + */ + +var server = http.createServer(api.app); + +/** + * Listen on provided port, on all network interfaces. + */ + +server.listen(port); +server.on('error', onError); +server.on('listening', onListening); + +/** + * Normalize a port into a number, string, or false. + */ + +function normalizePort(val) { + var port = parseInt(val, 10); + + if (isNaN(port)) { + // named pipe + return val; + } + + if (port >= 0) { + // port number + return port; + } + + return false; +} + +/** + * Event listener for HTTP server "error" event. + */ + +function onError(error) { + if (error.syscall !== 'listen') { + throw error; + } + + var bind = typeof port === 'string' + ? 'Pipe ' + port + : 'Port ' + port + + // handle specific listen errors with friendly messages + switch (error.code) { + case 'EACCES': + console.error(bind + ' requires elevated privileges'); + process.exit(1); + break; + case 'EADDRINUSE': + console.error(bind + ' is already in use'); + process.exit(1); + break; + default: + throw error; + } +} + +/** + * Event listener for HTTP server "listening" event. + */ + +function onListening() { + var addr = server.address(); + var bind = typeof addr === 'string' + ? 'pipe ' + addr + : 'port ' + addr.port; + console.log('Listening on ' + bind); +} diff --git a/api/config.js b/api/config.js new file mode 100644 index 00000000..89a83ace --- /dev/null +++ b/api/config.js @@ -0,0 +1,7 @@ +'use strict'; + +var env = process.env; + +module.exports = { + port: env.BITCORED_HTTP_PORT || 8000 +} diff --git a/api/routes/index.js b/api/routes/index.js new file mode 100644 index 00000000..3d220bc4 --- /dev/null +++ b/api/routes/index.js @@ -0,0 +1,11 @@ +'use strict'; + +var express = require('express'); +var router = express.Router(); + +/* GET home page. */ +router.get('/', function(req, res, next) { + res.send('index'); +}); + +module.exports = router; diff --git a/package.json b/package.json index 957d0d66..ae41b05d 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ "commander": "^2.3.0", "config": "^1.12.0", "cron": "^1.0.4", - "express": "~3.4.7", + "express": "4.11.1", "glob": "*", "js-yaml": "^3.2.7", "leveldown": "~0.10.0",