refactor: www -> es6 syntax

This commit is contained in:
Khaled Garbaya
2017-09-20 12:06:27 +02:00
committed by Benedikt Rötsch
parent dc3e8628f3
commit 3f6efdf750
2 changed files with 30 additions and 31 deletions

53
bin/www
View File

@@ -4,49 +4,48 @@
* Module dependencies. * Module dependencies.
*/ */
var app = require('../app'); const app = require('../app')
var debug = require('debug')('example-contentful-university-js:server'); const http = require('http')
var http = require('http');
/** /**
* Get port from environment and store in Express. * Get port from environment and store in Express.
*/ */
var port = normalizePort(process.env.PORT || '3000'); const port = normalizePort(process.env.PORT || '3000')
app.set('port', port); app.set('port', port)
/** /**
* Create HTTP server. * Create HTTP server.
*/ */
var server = http.createServer(app); const server = http.createServer(app)
/** /**
* Listen on provided port, on all network interfaces. * Listen on provided port, on all network interfaces.
*/ */
server.listen(port); server.listen(port)
server.on('error', onError); server.on('error', onError)
server.on('listening', onListening); server.on('listening', onListening)
/** /**
* Normalize a port into a number, string, or false. * Normalize a port into a number, string, or false.
*/ */
function normalizePort (val) { function normalizePort (val) {
var port = parseInt(val, 10); const port = parseInt(val, 10)
if (isNaN(port)) { if (isNaN(port)) {
// named pipe // named pipe
return val; return val
} }
if (port >= 0) { if (port >= 0) {
// port number // port number
return port; return port
} }
return false; return false
} }
/** /**
@@ -55,25 +54,25 @@ function normalizePort(val) {
function onError (error) { function onError (error) {
if (error.syscall !== 'listen') { if (error.syscall !== 'listen') {
throw error; throw error
} }
var bind = typeof port === 'string' const bind = typeof port === 'string'
? 'Pipe ' + port ? 'Pipe ' + port
: 'Port ' + port; : 'Port ' + port
// handle specific listen errors with friendly messages // handle specific listen errors with friendly messages
switch (error.code) { switch (error.code) {
case 'EACCES': case 'EACCES':
console.error(bind + ' requires elevated privileges'); console.error(bind + ' requires elevated privileges')
process.exit(1); process.exit(1)
break; break
case 'EADDRINUSE': case 'EADDRINUSE':
console.error(bind + ' is already in use'); console.error(bind + ' is already in use')
process.exit(1); process.exit(1)
break; break
default: default:
throw error; throw error
} }
} }
@@ -82,9 +81,9 @@ function onError(error) {
*/ */
function onListening () { function onListening () {
var addr = server.address(); const addr = server.address()
var bind = typeof addr === 'string' const bind = typeof addr === 'string'
? 'pipe ' + addr ? 'pipe ' + addr
: 'port ' + addr.port; : 'port ' + addr.port
console.log('Listening on ' + bind); console.log('Listening on ' + bind)
} }

View File

@@ -6,7 +6,7 @@
"start:watch": "nodemon ./bin/www --ignore public/", "start:watch": "nodemon ./bin/www --ignore public/",
"start": "node ./bin/www", "start": "node ./bin/www",
"lint": "eslint ./app.js routes", "lint": "eslint ./app.js routes",
"format": "eslint --fix . --ignore public node_modules" "format": "eslint --fix . bin --ignore public node_modules"
}, },
"dependencies": { "dependencies": {
"body-parser": "~1.15.2", "body-parser": "~1.15.2",