nodejs:启动服务器并查找字符串“server started at 127.0.0.1:8000";然后开始测试 [英] nodejs: Start server and look for string "server started at 127.0.0.1:8000" and then start the tests

查看:76
本文介绍了nodejs:启动服务器并查找字符串“server started at 127.0.0.1:8000";然后开始测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am starting the server from my NodeJS script and then want to execute my tests. But when I start the server the server start process doesn't return as it is running and control is not returned back. The server starts and the app can be accessed on http://localhost:8000.

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

trying to start the server and then monitor the stdout of the runserver process and look for a match "Starting server at 127.0.0.1:8000" and then proceed to run the tests. Is there any way it can be done using exec or spawn node command and then monitor for the required string to start my tests ?

基于之前的问题 此处 在 Http://localhost:8000 启动并运行时开始轮询测试.

Based on previous question here where starting test on polling when Http://localhost:8000 is up and running.

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

Solution I am looking for is Starting test based on stdout data string matches - "Starting development server".

推荐答案

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

Yes, use spawn and look for the string, then run your tests, monitor SIGTERM and SIGINT, then pass it along to the child.

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 信号.

The test server script used was the following, note above that it's passing back the SIGINT signal it received.

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)
    })
  }
}

这篇关于nodejs:启动服务器并查找字符串“server started at 127.0.0.1:8000";然后开始测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆