var express = require('express'); var spawn = require('child_process').spawn; var fs = require('fs'); var app = express(); app.get('/', function (req, res) { var phrase = req.param('phrase'); phrase = phrase || '恍恍惚'; fs.writeFile("D:\\Work\\nodejsprojects\\rnn-chat-backend\\input.txt", phrase, function(err) { if (err) { throw err; } console.log("The file was saved!"); var child = spawn('D:\\Work\\nodejsprojects\\rnn-chat-backend\\script.bat'); child.stdout.on('data', function(data) { console.log('stdout: ' + data); }); child.stderr.on('data', function(data) { console.log('stderr: ' + data); //Here is where the error output goes }); child.on('close', function(code) { console.log('closing code: ' + code); //Here you can get the exit code of the script fs.readFile('C:\\Users\\MACHENIKE\\Downloads\\char-rnn-chinese-master\\temp.txt', function read(err, data) { if (err) { throw err; } res.status(200); res.header('Content-Type', 'text/html; charset=utf-8'); res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.write(data); res.end(); }); }); }); }); app.listen(3001, function () { console.log('Example app listening on port 3001!'); }); module.exports = app;