【问题标题】:nodejs: Start server and look for string "server started at 127.0.0.1:8000" and then start the testsnodejs:启动服务器并查找字符串“服务器开始于 127.0.0.1:8000”,然后开始测试
【发布时间】:2021-08-21 15:38:46
【问题描述】:

我正在从我的 NodeJS 脚本启动服务器,然后想要执行我的测试。但是当我启动服务器时,服务器启动过程不会因为它正在运行而返回,并且控制不会返回。服务器启动,可以在 http://localhost:8000 访问应用。

尝试启动服务器,然后监控 runserver 进程的标准输出并寻找匹配的“Starting server at 127.0.0.1:8000”,然后继续运行测试。 有什么方法可以使用 exec 或 spawn node 命令完成,然后监视所需的字符串以开始我的测试?

基于上一个问题here where 在 Http://localhost:8000 启动并运行时开始轮询测试。

我正在寻找的解决方案是根据标准输出数据字符串匹配启动测试 - “启动开发服务器”。

【问题讨论】:

  • 你应该将链接返回到prev question,或者添加你尝试过的代码,然后它会提供更多的上下文并且可能不会招致一堆downvotes

标签: node.js server promise stdout


【解决方案1】:

是的,使用 spawn 并查找字符串,然后运行您的测试,监控 SIGTERM 和 SIGINT,然后将其传递给孩子。

const {
  spawn
} = require('child_process')

// your cmd to start the server, possibly spawn('python', ['manage.py', 'startserver'])
const server = spawn('node', ['server.js'])

let timer = null
server.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`)

  // look for the string in stdout
  if (data.includes('Starting development server')) {

    console.log('Commencing tests in 2 seconds')
    timer = setTimeout(() => {
      console.log('Run tests')

      // ...

      // tests complete
      setTimeout(() => {
        console.log('Tests completed, shutting down server')
        server.kill('SIGINT')
      }, 2000)
    }, 2000)
  }
})

server.stderr.on('data', (data) => {
  clearTimeout(timer)
  console.error(`stderr: ${data}`)
});

server.on('close', (code) => {
  clearTimeout(timer)
  console.log(`child process exited with code ${code}`);
});

process
  .on('SIGTERM', shutdown('SIGTERM'))
  .on('SIGINT', shutdown('SIGINT'))
  .on('uncaughtException', shutdown('uncaughtException'))

function shutdown(signal) {
  return (err) => {
    console.log(`\n${signal} signal received.`)

    if (err && err !== signal) console.error(err.stack || err)

    console.log('Killing child process.')
    server.kill(signal)
  }
}

结果

node spawn.js 
stdout: Starting development server http://localhost:8000

Commencing tests in 2 seconds
Run tests
Tests completed, shutting down server
stdout: 
SIGINT signal received.

stdout: Closing HTTP server.

stdout: HTTP server closed.

child process exited with code 0

使用的测试服务器脚本如下,请注意上面它正在传回它收到的 SIGINT 信号。

const express = require('express')
const app = express()
const port = 8000

app.get('/', (req, res) => res.send('Hello World!'))

const server = app.listen(port, () => console.log(`Starting development server http://localhost:${port}`))


process
  .on('SIGTERM', shutdown('SIGTERM'))
  .on('SIGINT', shutdown('SIGINT'))
  .on('uncaughtException', shutdown('uncaughtException'))

function shutdown(signal) {
  return (err) => {
    console.log(`\n${signal} signal received.`)

    if (err && err !== signal) console.error(err.stack || err)

    console.log('Closing HTTP server.')
    server.close(() => {
      console.log('HTTP server closed.')
      //
      process.exit(err && err !== signal ? 1 : 0)
    })
  }
}

【讨论】:

  • 感谢劳伦斯,解决方案奏效了。我不得不做一些改变,因为我想从我的测试中调用它。所以我创建了一个函数并添加了超时逻辑,然后从我的测试中调用以启动服务器,然后在测试完成时调用以杀死/停止服务器作为另一个函数。
猜你喜欢
  • 2019-05-30
  • 2015-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多