i node js project in which i call python script. The problem is that it displays only part of the output and the server drops with error ERR_HTTP_HEADERS_SENT.
hello.py
import sys
for i in range(3000):
print(i)
sys.stdout.flush()
index.js
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
app.set('view engine', 'ejs')
app.use(bodyParser.urlencoded({ limit: '10mb', extended: false }))
app.get("/", (req, res) => {
const spawn = require("child_process").spawn
const pythonProcess = spawn('python', ["./hello.py"])
pythonProcess.stdout.on('data', (data) => {
results = data.toString().split("\r\n")
res.render("../index.ejs", {results})
})
})
app.listen(process.env.PORT || 3000)
index.ejs
<h2>Results:</h2>
<% results.forEach(result => { %>
<div><%= result %> </div>
<% }) %>
The result is page with numbers from 0 to 1550, each on separate line, but it should be 0 to 3000, what's wrong?