-- This code is a section of nodejs startup.
--The code comments have not been completed, and there are inappropriate comments.
/** * Module dependencies. */ var app = require('../app'); var debug = require('debug')('untitled:server'); var http = require('http'); /** * Get port from environment and store in Express. * 80 ports can be modified depending on your needs */ var port = normalizePort(process.env.PORT || '80'); app.set('port', port); /** * Create HTTP server. */ var server = http.createServer(app); /** * Listen on provided port, on all network interfaces. *Turn on listening - you can specify the port number and IP *Note here that if the server hosts file does not map 127.0.0.1 and localhost, the following IP may be required, otherwise you know *thanks @IPPP *As for public network mapping, ngrok tool is recommended */ server.listen(port,'127.0.0.1'); console.log("Service started~~~"); //Service error execute onError function server.on('error', onError); //Service listening status and its address port information - don't know how to use it yet! 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 terminated 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; debug('Listening on ' + bind); console.log('Listening on ' + bind); }
---------------------------------------------------------------------------------------------------------------------
*、@IPPP
The inspiration for solving the IP problem comes from https://my.oschina.net/tearlight/blog/195543 great The problem that has arisen is: if you do not provide hostname, you can only use localhost to access. Later, you can access it in three ways without providing hostname. It is strange to say!