nodejs express服务器意外停止

时间:2018-11-26 11:16:04

标签: node.js express socket.io

我有一台内置Express和套接字IO的服务器。问题在于,每次服务器在第一个客户端连接后2分钟才停止响应。我已经完成/检查过的事情:

  • 快捷日志未显示任何错误。
  • socket.IO日志在服务器端没有显示错误。
  • socket.IO日志在客户端上显示ping超时(几次成功ping之后)。
  • 听uncaughtException / UnhandeledRejection没有帮助。

因此,当我启动服务器并通过浏览器GET / login进行任何操作时,服务器将在约2分钟后停止自行响应,并且关闭服务器的唯一方法是关闭cmd(CTRL即使听了SIGINT,+ C也不起作用。

我将在此处发布代码的相关部分:

server.js

const http = require('http');
const express = require('express');
const socketIO = require('socket.io');
let app = express();
let server = http.createServer(app);
var io = socketIO(server);

app.get(`/login`, (req, res) => {
    console.log(`got GET /login`);
    res.sendFile(publicPath + '/login.html');
});

io.on(`connection`, (socket) => {
    socket.on(`login`, async (details, cb) => {
      login(details.username, details.password, async (res) => {
        if (res === true) {
          let user = new User(details.username, details.password);
          let token = await user.generateAuthToken();

          cb(`/set?token=${token}`);
        } else if (res === false) {
          socket.emit(`loginFailed`, 'Password incorrect. Please try again!')
        } else if (res === `Username doesn't exist`) {
          socket.emit(`loginFailed`, res)
        }
      });
    });

    socket.on('getAllLeads', async (callback) => {
      leads = await getAllLeads();
      callback(leads);
    });

    socket.on('getSomeLeads', async (options, callback) => {
      leads = await getSomeLeads(options);
      callback(leads);
    });

    socket.on(`newFile`, ({text, words, options, fileName}) => {
      console.log('Starting...');
      words.forEach((wordSet, index) => {
        wordSet.forEach(word => {
          regexp = `\\s+${word}+\\s`;
          re = new RegExp(regexp);
          text = text.replace(re, `${options[index]}`);
      });
    });
    fs.writeFileSync(`${fileName}.txt`, text, 'utf8');
    console.log(`Finished writing to ${fileName}.txt successfully!`);
  });
});

server.listen(3000, () => {
      console.log(chalk.yellow(`Server is up on port ${port}`));
});

login.js

const socket = io();

socket.on(`reconnect_error`, (err) => {
  console.log(err);
});

socket.on(`connect`, () => {
  console.log('connected to login page');
  if (getQueryVariable('failed')) {
    $('.error, .error p').css('opacity', 1);
    $('.error p').text('Unauthorized! PLEASE log in.');
  }

  socket.on(`loginFailed`, (reason) => {
    $('.error, .error p').css('opacity', 1);
    $('.error p').text(reason);
    console.log(`got login failed`);
  });

  $('#form').submit((e) => {
    e.preventDefault();

    let username = $('#username').val();
    let password = $('#pass').val();

    socket.emit(`login`, {username, password}, (path) => {
      window.location.href = path;
    });
  });
});

function getQueryVariable(variable)
{
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
          var pair = vars[i].split("=");
          if (pair[0] == variable) return pair[1];
  }
  return (false);
}

0 个答案:

没有答案