I start Python programs via Node.js and send a request to the server. After the Python program has run, I want to output the data on the client side. Python takes a long time, but it will stdout the data sometime.
After some time I am getting following error message in your browser:
GET http://localhost:4200/api/test net::ERR_EMPTY_RESPONSE core.js:1673 ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: null, ok: false, …}
But my Python program went through.
I tried with res.end(), res.send(200).json() and res.send() but I am getting the same problem
server.ts
import app from './app';
import { Database } from './database';
const PORT = 8000;
Database.connect();
app.listen(PORT, () => {
console.log('Express server listening on port ' + PORT);
});
app.timeout = 12000000000;
Node.js route.ts
app.route('/api/test').get((req: Request, res: Response) => {
const spawn = require('child_process').spawn;
const ls = spawn('python', [__dirname + '/getPosts.py']);
let inData;
ls.stdout.on('data', (chunk) => {
inData = chunk.toString().replace('\n', '').split(',');
});
ls.stderr.on('data', (chunk) => {
console.log(`stderr: ${chunk}`);
});
ls.on('close', (code) => {
res.json(inData[0]); // -> ['True']
});
});
Angular service.ts
callData() {
return this._http.get<any>(`${'/api/test'}`, {}).pipe(map((res: any) => {
return res;
}));
}