Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript NodeError:在NodeJS中结束后写入_Javascript_Node.js - Fatal编程技术网

Javascript NodeError:在NodeJS中结束后写入

Javascript NodeError:在NodeJS中结束后写入,javascript,node.js,Javascript,Node.js,我开始学习NodeJS,并开始制作一个小应用程序,根据用户的点击请求对文件进行加密和解密。下面的代码在第一次加密请求时运行良好,但在另一次加密请求时崩溃 App.js var fs = require('fs') var crypto = require('crypto') const express = require('express') const app = express() const port = 3000 var key = '1bd'; var cipher = crypto.

我开始学习NodeJS,并开始制作一个小应用程序,根据用户的点击请求对文件进行加密和解密。下面的代码在第一次加密请求时运行良好,但在另一次加密请求时崩溃

App.js

var fs = require('fs')
var crypto = require('crypto')
const express = require('express')
const app = express()
const port = 3000
var key = '1bd';
var cipher = crypto.createCipher('aes-256-cbc', key);
var decipher = crypto.createDecipher('aes-256-cbc', key);


app.use(express.static('public'))


app.get('/', (req, res) => {
    res.sendFile('/enc-dec.html', { root: __dirname })
})

app.post('/encrypt', (req, res) => {
    fs.createReadStream('input.txt')
    .pipe(cipher)
    .pipe(fs.createWriteStream('input.txt.enc'))
    .on('finish', function() {
        res.end("Encrypted")
    });
})

app.listen(port, () => console.log(`App listening on port ${port}!`))
错误:

NodeError: write after end
    at writeAfterEnd (_stream_writable.js:237:12)
    at Cipher.Writable.write (_stream_writable.js:287:5)
    at ReadStream.ondata (_stream_readable.js:646:20)
    at ReadStream.emit (events.js:180:13)
    at addChunk (_stream_readable.js:269:12)
    at readableAddChunk (_stream_readable.js:256:11)
    at ReadStream.Readable.push (_stream_readable.js:213:10)
    at fs.read (fs.js:2123:12)
    at FSReqWrap.wrapper [as oncomplete] (fs.js:680:17)
Emitted 'error' event at:
    at Cipher.onerror (_stream_readable.js:670:12)
    at Cipher.emit (events.js:180:13)
    at writeAfterEnd (_stream_writable.js:239:10)
    at Cipher.Writable.write (_stream_writable.js:287:5)
    [... lines matching original stack trace ...]
    at fs.read (fs.js:2123:12)

我不是这方面的专家,但它似乎与fs有关。任何帮助都将不胜感激。谢谢

您的问题是重复使用同一密码多次。在流中使用一次后,就不能再使用;必须创建一个新的

您应该在请求处理程序中创建密码,如下所示:

app.post('/encrypt', (req, res) => {
    var cipher = crypto.createCipher('aes-256-cbc', key);
    fs.createReadStream('input.txt')
    .pipe(cipher)
    .pipe(fs.createWriteStream('input.txt.enc'))
    .on('finish', function() {
        res.end("Encrypted")
    });
})
看起来您还没有完成解密功能,但在构建该功能时,还需要移动
var decipher=crypto.createDecipher('aes-256-cbc',key)
下放到请求处理程序中,因为解密也不能重用