1

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

集群.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;
}

服务器.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
      ^

任何想法为什么?

4

1 回答 1

2

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

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

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

如果您有结果,kill则需要处理 ( kill <pid>)。

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

$ pm2 kill
Daemon killed

验证 Windows:

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

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

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

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

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

如果您知道要终止的进程的名称,对于examplenotepad.exe,请在命令提示符下使用以下命令来结束它:

taskkill /IM notepad.exe

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

taskkill /PID 827
于 2017-05-04T08:33:16.580 回答