Skip to content

Instantly share code, notes, and snippets.

@robert52
Created April 27, 2016 15:18
Show Gist options
  • Save robert52/e705727c047295297af35cf7e24eb203 to your computer and use it in GitHub Desktop.
Save robert52/e705727c047295297af35cf7e24eb203 to your computer and use it in GitHub Desktop.
// Get process environment or set default environment to development
const ENV = process.env.NODE_ENV || 'development';
const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
let server;
app.set('root', __dirname);
app.set('env', ENV);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.disable('x-powered-by');
app.get('/api/status', (req, res, next) => {
res.json({ message: 'API is running.' });
});
app.use((err, req, res, next) => {
console.error(err);
res.status(500).json(err);
});
if (!module.parent) {
server = http.createServer(app);
server.listen(config.port || 3000, config.hostname, () => {
let addr = server.address();
console.info('---');
console.info('%s is running.', config.app.name);
console.info('Hostname: %s', addr.address);
console.info('Port: %s', addr.port);
console.info('Environment: %s', ENV.toLowerCase());
console.info('Access: %s', config.baseUrl);
console.info('---');
});
}
module.exports = app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment