| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- #!/usr/bin/env node
- 'use strict';
- /*
- *
- * Copyright 2016-2017 Red Hat, Inc, and individual contributors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
- /**
- * Module dependencies.
- */
- const logger = require('../logger.js');
- const app = require('../app');
- const http = require('http');
- /**
- * Get port from environment and store in Express.
- */
- const port = normalizePort(process.env.PORT || '8080');
- app.set('port', port);
- /**
- * Create HTTP server.
- */
- const server = http.createServer(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) {
- const 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;
- }
- const bind = typeof port === 'string'
- ? 'Pipe ' + port
- : 'Port ' + port;
- // handle specific listen errors with friendly messages
- switch (error.code) {
- case 'EACCES':
- logger.error(bind + ' requires elevated privileges');
- process.exit(1);
- case 'EADDRINUSE':
- logger.error(bind + ' is already in use');
- process.exit(1);
- default:
- throw error;
- }
- }
- /**
- * Event listener for HTTP server "listening" event.
- */
- function onListening () {
- const addr = server.address();
- const bind = typeof addr === 'string'
- ? 'pipe ' + addr
- : 'port ' + addr.port;
- logger.info('Listening on ' + bind);
- }
|