简体   繁体   中英

node js server doesn't get started

I have a server.js file that will be used to start a node server and do some tasks. But when i run the file, the server doesn't getting started. Even i have catch the error it doesn't give me any errors. Any help would be appriciated!

Thanks.

below is my server.js file code block,

var express = require('express');
var app = express();
var request = require('request');
var fs = require('fs');  

//--ssl files for the socket.io
var options = {

key: fs.readFileSync('pre3private.key'),

cert: fs.readFileSync('pre3cert.cert')

};
//--/ssl files for the socket.io 

var https = require('https').Server(options, app);
var io = require('socket.io')(https);
var net = require('net'); 

var check_port = 8081; 

var server = net.createServer();

server.once('error', function(err) {
 console.log(err);
 if (err.code === 'EADDRINUSE') {
  // port is currently in use
  check_port += 1;
  if(check_port === 8050) { check_port = 8000; }
  server.listen(check_port);
 }
});

server.once('listening', function() {
 // close the server if listening doesn't fail
 server.close();

 var port = process.env.PORT || check_port;

 https.listen(port, function(){
  debug('Server started! at port ' + port);
 });                 

});

You need to call server.listen(check_port); at the end of your code, to try the first port

First, you need to move the 'https.listen(' part outside of "server.once('listening'...", but I think that there are other issues in that code.

I recommend to start with a clean example from https://nodejs.org/api/net.html (or express) run it without changes to see that it works and then add your changes incrementally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM