我正在学习Node.js,我刚刚创建了一个表单,当我发出post请求时,我的节点js脚本将执行一个终端命令来使用gradle进行构建。默认情况下,我使用index.html来显示表单,一旦构建完成,我希望显示build.html页面。
但我得到了一个错误,
我的代码:
const http = require('http');
const fs = require('fs');
const { spawn } = require("child_process");
var qs = require('querystring');
const server = http.createServer((req, res)=> {
if (req.method == 'POST') {
var body = '';
console.log("Post received");
req.on('data', function (data) {
body += data;
var post = qs.parse(body);
// Too much POST data, kill the connection!
// 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
if (body.length > 1e6)
req.connection.destroy();
});
req.on('end', function () {
var post = qs.parse(body);
// use post['blah'], etc.
const ls = spawn("./gradlew", ["build"]);
ls.stdout.on("data", data => {
// console.log(`stdout: ${data}`);
});
ls.stderr.on("data", data => {
console.log(`stderr: ${data}`);
});
ls.on('error', (error) => {
console.log(`error: ${error.message}`);
});
ls.on("close", code => {
//console.log(`child process exited with code ${code}`);
console.log("Build success");
const readStream = fs.createReadStream('./build.html');
res.writeHead(200, {'Content-type': 'text/html'});
readStream.pipe(res);
});
});
}
const readStream = fs.createReadStream('./index.html');
res.writeHead(200, {'Content-type': 'text/html'});
readStream.pipe(res);
});
server.listen('3000');
错误:
events.js:183
throw er; // Unhandled 'error' event
^
Error: write after end
at write_ (_http_outgoing.js:622:15)
at ServerResponse.write (_http_outgoing.js:617:10)
at ReadStream.ondata (_stream_readable.js:639:20)
at emitOne (events.js:116:13)
at ReadStream.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at ReadStream.Readable.push (_stream_readable.js:208:10)
at fs.read (fs.js:2051:12)
at FSReqWrap.wrapper [as oncomplete] (fs.js:658:17)
如何修复此错误,如何在构建成功后显示build.html页面。
提前感谢!
发布于 2021-02-10 00:06:06
错误堆栈上正在显示错误消息字符串:
Error: write after end
readable.pipe
在可写流上调用end
。在这种情况下,我相信您的两个文件读取流之间存在竞争条件。
/*****************************************************/
/* First Stream */
/*****************************************************/
console.log("Build success");
const readStream = fs.createReadStream('./build.html');
res.writeHead(200, {
'Content-type': 'text/html'
});
readStream.pipe(res);
/*****************************************************/
/* Second Stream */
/*****************************************************/
const readStream = fs.createReadStream('./index.html');
res.writeHead(200, {'Content-type': 'text/html'});
readStream.pipe(res);
我相信如果最后一个流不是POST
,你会想要发送它,所以你可以为任何其他类型的请求添加else子句:
const http = require('http');
const fs = require('fs');
const {
spawn
} = require("child_process");
var qs = require('querystring');
const server = http.createServer((req, res) => {
if (req.method == 'POST') {
var body = '';
console.log("Post received");
req.on('data', function(data) {
body += data;
var post = qs.parse(body);
// Too much POST data, kill the connection!
// 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
if (body.length > 1e6)
req.connection.destroy();
});
req.on('end', function() {
var post = qs.parse(body);
// use post['blah'], etc.
const ls = spawn("./gradlew", ["build"]);
ls.stdout.on("data", data => {
// console.log(`stdout: ${data}`);
});
ls.stderr.on("data", data => {
console.log(`stderr: ${data}`);
});
ls.on('error', (error) => {
console.log(`error: ${error.message}`);
});
ls.on("close", code => {
//console.log(`child process exited with code ${code}`);
console.log("Build success");
const readStream = fs.createReadStream('./build.html');
res.writeHead(200, {
'Content-type': 'text/html'
});
readStream.pipe(res);
});
});
} else {
const readStream = fs.createReadStream('./index.html');
res.writeHead(200, {
'Content-type': 'text/html'
});
readStream.pipe(res);
}
});
server.listen('3000')
https://stackoverflow.com/questions/66122333
复制相似问题