You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Travis CI中启动Node服务器后将其关闭?

Fixing Travis CI to Test Dependencies, Verify Server, and Build

Hey there! Let's tweak your Travis CI setup to cover all your requirements: installing all dependencies, verifying the server runs properly, shutting it down, and then completing the build. Here's a step-by-step breakdown:

1. Updated .travis.yml Configuration

First, let's adjust your CI config to handle server startup, validation, shutdown, and building. We'll add tools to wait for the server and ensure MongoDB is ready before proceeding:

language: node_js
node_js:
  - "stable"
services:
  - mongodb
install:
  - npm install # Install root project dependencies
  - (cd client/ && npm install) # Install client-side dependencies
  - (cd client/ && npm install wait-on --save-dev) # Add tool to wait for server readiness
before_script:
  - cd client/
  - # Wait for MongoDB to fully initialize (prevents early connection failures)
    until mongo --eval "db.adminCommand('ping')"; do sleep 1; done
script:
  - npm start & # Launch frontend + backend servers in the background
  - wait-on http://localhost:3000 # Wait for frontend to be accessible (adjust port if needed)
  - wait-on http://localhost:5000 # Wait for backend to be accessible (adjust port if needed)
  - npm test # Run your test suite to verify server functionality
  - # Cleanly stop both servers to free resources for build
    pkill -f "node scripts/start.js"
    pkill -f "node ../bin/www"
  - npm run build # Execute production build step

Key Changes Explained:

  • wait-on: This package waits until your server endpoints are responsive, ensuring we don't run tests or shut down too early.
  • Background Server Start: npm start & runs the servers in the background so Travis can proceed to subsequent steps instead of hanging indefinitely.
  • MongoDB Wait Loop: The until mongo ... command ensures MongoDB is fully ready before your backend tries to establish a connection.
  • Server Shutdown: pkill stops both frontend and backend processes cleanly, preventing conflicts during the build step.

2. Optional: Add a Stop Script to package.json

For a cleaner, more maintainable shutdown command, add a dedicated stop script to your client's package.json:

"scripts": {
  "start": "concurrently \"node scripts/start.js\" \"node ../bin/www\"",
  "build": "node scripts/build.js",
  "test": "...",
  "stop": "pkill -f 'node scripts/start.js' && pkill -f 'node ../bin/www'"
}

Then replace the two pkill lines in your .travis.yml with:

- npm run stop

Quick Adjustments for Your Project:

  • Port Numbers: Double-check the URLs in wait-on to match your actual frontend/backend ports (e.g., if your frontend runs on 8080, update to http://localhost:8080).
  • Test Command: Ensure your npm test script is configured to run all necessary tests that validate server functionality.
  • Process Names: If your server processes have different names, adjust the pkill arguments to match exactly what runs locally (use ps aux to check).

内容的提问来源于stack exchange,提问作者Keagansed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:10:43