I am starting the server from my 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. so I have used nodejs fetch and http request to check if response==200 at http://localhost:8000 but get error: console.error FetchError { message: 'request to http://127.0.0.1:8000/ failed, reason: connect ECONNREFUSED 127.0.0.1:8000', type: 'system', errno: 'ECONNREFUSED', code: 'ECONNREFUSED' } Is there any way I could poll and start my tests ? My code:
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const fetch = require('node-fetch');
const http = require('http');
async function waitforlocalhost() {
await fetch('http://127.0.0.1:8000/'), (response) => {
//http.get({hostname:'localhost',port: 8000,path: '/',method: 'GET',agent: false}, (response) => {
if (response.status === 200) {
resolve();
return;
} else {
setTimeout(waitforlocalhost,2000);
}
}
}
class CreateEnv {
async Execute () {
try {
//create database and start server
console.log("Create database and runserver...");
exec('python manage.py startserver');
await waitforlocalhost();
console.log("Server started...");
} catch (e) {
console.error(e);
console.log("Error creating database and runserver...");
return;
}
}
}
module.exports = CreateEnv;