Commit 0a549d09 authored by toby's avatar toby

de-side-effect app

parent 1163bfa5
const debug = require('debug')('loafer:server');
const http = require('http');
const exec = require('child_process').exec;
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const routes = require('./routes/index');
const app = express();
/**
* Get port and normalize into a number, string, or false.
*/
function normalizedPort() {
const val = process.env.PORT || '3011';
const port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
function configureApp(port) {
app.set('port', port);
app.set('views', path.join(__dirname, '/./views'));
app.set('view engine', 'pug');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, '/./public')));
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
return app;
}
function httpListen(app) {
const server = http.createServer(app);
const port = app.get('port');
server.listen(port);
server.on('error', onError.bind(port));
server.on('listening', onListening.bind(port));
}
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof this === 'string'
? 'Pipe ' + this
: 'Port ' + this;
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;
}
}
function onListening() {
exec('open http://localhost:' + this);
}
function init() {
httpListen(configureApp(normalizedPort()));
}
module.exports.init = init;
#!/usr/bin/env node
/**
* Module dependencies.
*/
const app = require('../app');
const debug = require('debug')('loafer:server');
const http = require('http');
const exec = require('child_process').exec;
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const routes = require('../routes/index');
const app = express();
/**
* Get port and normalize into a number, string, or false.
*/
function normalizedPort() {
const val = process.env.PORT || '3011';
const port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
function configureApp(port) {
app.set('port', port);
app.set('views', path.join(__dirname, '/../views'));
app.set('view engine', 'pug');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, '/../public')));
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
return app;
}
function httpListen(app) {
const server = http.createServer(app);
const port = app.get('port');
server.listen(port);
server.on('error', onError.bind(port));
server.on('listening', onListening.bind(port));
}
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof this === 'string'
? 'Pipe ' + this
: 'Port ' + this;
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;
}
}
function onListening() {
exec('open http://localhost:' + this);
}
httpListen(configureApp(normalizedPort()));
app.init();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment