Initial http server structure
This commit is contained in:
parent
ae13cb3b55
commit
845436c34b
44
api/app.js
Normal file
44
api/app.js
Normal file
@ -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;
|
||||
90
api/bin/www
Executable file
90
api/bin/www
Executable file
@ -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);
|
||||
}
|
||||
7
api/config.js
Normal file
7
api/config.js
Normal file
@ -0,0 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
var env = process.env;
|
||||
|
||||
module.exports = {
|
||||
port: env.BITCORED_HTTP_PORT || 8000
|
||||
}
|
||||
11
api/routes/index.js
Normal file
11
api/routes/index.js
Normal file
@ -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;
|
||||
@ -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",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user