Node.js集群错误

时间:2017-05-04 08:09:14

标签: javascript node.js express node-cluster

您好我是node.js和javascript的新手,我正在尝试使用nodejs集群模块创建一个culster.js,在我的if语句结束时,我正在调用server.js来启动应用程序。

cluster.js

const cluster = require('cluster');
const cpuCount = require('os').cpus().length;
const startServer = require('./server');

if (cluster.isMaster) {
  for (let i = 0; i < cpuCount; i += 1) {
    cluster.fork();
  }
  cluster.on('exit', () => {
    cluster.fork();
  });
} else {
  return startServer;
}

server.js

const fs = require('fs');
const path = require('path');

const express = require('express');
const auth = require('http-auth');
const {
  createBundleRenderer,
} = require('vue-server-renderer');

const bundle = fs.readFileSync('dist/server.js', 'utf-8');
const renderer = createBundleRenderer(bundle);

function parseIndexHtml() {
  const [
    entire,
    htmlOpen,
    htmlOpenTailAndHead,
    headCloseAndBodyOpen,
    bodyOpenTailAndContentBeforeApp,
    contentAfterAppAndHtmlClose,
  ] = fs.readFileSync('index.html', 'utf8').match(/^([\s\S]+?<html)([\s\S]+?)(<\/head>[\s\S]*?<body)([\s\S]+?)<div id="?app"?><\/div>([\s\S]+)$/);

  return {
    entire,
    htmlOpen,
    htmlOpenTailAndHead,
    headCloseAndBodyOpen,
    bodyOpenTailAndContentBeforeApp,
    contentAfterAppAndHtmlClose,
  };
}

const indexHtml = parseIndexHtml();
const app = express();

const basicAuth = auth.basic({
  realm: 'Jobportal',
}, (username, password, callback) => {
  callback(username === 'x' && password === 'x');
});

app.get('/ping', (request, response) => {
  response.status(200).end();
});

app.use(auth.connect(basicAuth));

// serve pure static assets
app.use('/public', express.static(path.resolve('./public')));
app.use('/dist', express.static(path.resolve('./dist')));

app.get('*', (request, response) => {
  const context = {
    url: request.url,
  };

  renderer.renderToString(context, (error, html) => {
    if (error) {
      if (error.code === '404') {
        response.status(404).end(indexHtml.entire);
      } else {
        response.status(500).end(indexHtml.entire);
        console.error(`Error during render: ${request.url}`); // eslint-disable-line
        console.error(error); // eslint-disable-line
      }
      return;
    }

    const {
      title,
      htmlAttrs,
      bodyAttrs,
      link,
      style,
      script,
      noscript,
      meta,
    } = context.meta.inject();

    response.write(
      `${indexHtml.htmlOpen} data-vue-meta-server-rendered ${htmlAttrs.text()} ${indexHtml.htmlOpenTailAndHead}
      ${meta.text()}
      ${title.text()}
      ${link.text()}
      ${style.text()}
      ${script.text()}
      <script>
        window.__INITIAL_STATE__ = ${JSON.stringify(context.initialState)}
      </script>
      ${noscript.text()}
      ${indexHtml.headCloseAndBodyOpen} ${bodyAttrs.text()} ${indexHtml.bodyOpenTailAndContentBeforeApp}
      ${html}
      <script src="/dist/client.js"></script>
      ${indexHtml.contentAfterAppAndHtmlClose}`
    );

    response.end();
  });
});

const port = 8181;

// start server
app.listen(port, () => {
  console.log(`server started at port ${port}`); // eslint-disable-line
});

我收到错误

server started at port 8181
events.js:163
      throw er; // Unhandled 'error' event
      ^

Error: bind EADDRINUSE null:8181
    at Object.exports._errnoException (util.js:1050:11)
    at exports._exceptionWithHostPort (util.js:1073:20)
    at listenOnMasterHandle (net.js:1336:16)
    at rr (internal/cluster/child.js:111:12)
    at Worker.send (internal/cluster/child.js:78:7)
    at process.onInternalMessage (internal/cluster/utils.js:42:8)
    at emitTwo (events.js:111:20)
    at process.emit (events.js:194:7)
    at process.nextTick (internal/child_process.js:766:12)
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
events.js:163
      throw er; // Unhandled 'error' event
      ^

任何想法为什么?

2 个答案:

答案 0 :(得分:1)

EADDRINUSE表示listen()尝试绑定服务器的端口号已在使用中。

您需要验证您的系统上是否已使用该端口。要做到这一点:

  • 在linux上:sudo netstat -nltp | grep (port)在您的情况下是端口8181。
  • 在OSX上:sudo lsof -i -P | grep (port)

如果您有结果,则需要kill该过程(kill <pid>)。

您应该检查pm2 list是否返回0进程。此外,当您执行pm2 stopAll时,套接字不会被释放。不要忘记做pm2 kill以确保守护程序被杀死。

$ pm2 kill
Daemon killed

验证Windows:

C:\> netstat -a -b
  • a显示所有连接和侦听端口。

  • b显示创建每个连接或侦听端口所涉及的可执行文件。在某些情况下,众所周知的可执行文件承载多个独立组件,在这些情况下,将显示创建连接或侦听端口所涉及的组件序列。在这种情况下,可执行文件名在底部的[]中,顶部是它调用的组件,依此类推,直到达到TCP / IP。请注意,此选项可能非常耗时,并且除非您具有足够的权限,否则将失败。

  • n以数字形式显示地址和端口号。

  • o显示与每个连接关联的拥有进程ID。

在Windows命令行中杀死的示例:

如果你知道要杀死的进程的名称,对于example notepad.exe,请在命令提示符下使用以下命令来结束它:

taskkill /IM notepad.exe

要终止进程的单个实例,请指定其进程ID(PID)。对于example,如果所需进程的PID为827,请使用以下命令将其终止:

taskkill /PID 827

答案 1 :(得分:1)

const throng = require('throng');

throng({
  master: () => {
    console.log('Started master');
  },
  start: (id) => {
    console.log(`Started worker ${id}`);
    require('./server');

    process.on('SIGTERM', () => {
      console.log(`Worker ${id} exiting...`);
      process.exit();
    });
  },
});

我不得不添加“throng”包https://www.npmjs.com/package/throng 并遵循http://eslint.org/docs/rules/

中的最新规则

现在对我有用