gpt4 book ai didi

node.js fs.createWriteStream 创建文件,但无法无错误地写入

转载 作者:太空宇宙 更新时间:2023-11-04 02:45:42 25 4
gpt4 key购买 nike

我正在 try catch “未捕获的异常”并将它们写入服务器上的文件中。我的代码是:

var cluster = require('cluster');
// The master's job to spawn workers initially and when they die

if (cluster.isMaster) {
// Get the number of processor cores
var cpuCount = require('os').cpus().length;

// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
}

// When a worker exits, fork a new one
cluster.on('exit', function(worker) {
console.log('Worker %d died', worker.id);
cluster.fork();
});
} else if (cluster.isWorker) {
const _express = require('express');
const _bodyParser = require('body-parser');
const _cors = require('cors');
const _fs = require('fs');
const _util = require('util');
const _app = _express();
const _http = require('http');
const _server = _http.createServer(_app);
const _port = 80;

process.on('uncaughtException', function(_err) {
var _error = _fs.createWriteStream(__dirname + '/node.error.log', { flags: 'a' });

_error.write(_err.message + '\n', (err) => {
if (err) {
console.log('Error:', err.message);
}else{
console.log('Error Written');
}
});

_error.end();

console.log('Caught exception: ' + _err);
});var _listener = _server.listen(_port, () => {
console.log('Server started on port ' + _port);
});

const _io = require('socket.io')(_listener);

// parse application/json
_app.use(_bodyParser.json());

// parse application/x-www-form-urlencoded
_app.use(_bodyParser.urlencoded({ extended: true }));

_app.use(_express.static(__dirname + '/node_modules'));

// Allow cross-origin requests
var corsOptions = {
origin: function(_origin, _callback){
_callback(null, _origin);
},
credentials: true
};

_app.use(_cors(corsOptions));

// html render engine
_app.engine('html', require('ejs').renderFile);
_app.set('view engine', 'html');

_io.on('connection', function (_socket) {
// Sends to the initiator
_socket.emit("connectionSuccessful", "Connected on socket " + _socket.id);

_socket.on('registerUser', function(_args) {
console.log('registerUser', _args);
fakeFunction();
});
});
}

registerUser 事件不是我的完整事件,而是为了引发错误而缩短的。

原创

底部的控制台日志被打印到命令行(捕获的异常行)。错误文件已创建(如果尚未创建)。但是,_error.write 区域内的任何内容都不会被执行。没有控制台日志显示,文件保持为空。

这不是权限问题,因为我已授予每个人 (666) 读取和写入文件的权限(用于测试)。所以我不确定为什么这不起作用。我见过的所有例子都表明这对他们有用。我不确定为什么它对我不起作用。

编辑

这是我在控制台中看到的内容:

Server started on port 80
Connected
Caught exception: TypeError: Cannot read property 'multi_id' of undefined
TypeError: Cannot read property 'multi_id' of undefined
at Query._callback (/var/www/nodejs/models/auctions.js:41:18)
at Query.Sequence.end (/var/www/nodejs/node_modules/mysql-clusterfarm/lib/protocol/sequences/Sequence.js:85:24)
at Query._handleFinalResultPacket (/var/www/nodejs/node_modules/mysql-clusterfarm/lib/protocol/sequences/Query.js:144:8)
at Query.EofPacket (/var/www/nodejs/node_modules/mysql-clusterfarm/lib/protocol/sequences/Query.js:128:8)
at Protocol._parsePacket (/var/www/nodejs/node_modules/mysql-clusterfarm/lib/protocol/Protocol.js:280:23)
at Parser.write (/var/www/nodejs/node_modules/mysql-clusterfarm/lib/protocol/Parser.js:74:12)
at Protocol.write (/var/www/nodejs/node_modules/mysql-clusterfarm/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/var/www/nodejs/node_modules/mysql-clusterfarm/lib/Connection.js:109:28)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7) 'Uncaught Exception thrown'
Worker 1 died

已解决

我发现我实际上捕获了两次 uncaughtExceptions...

process
.on('unhandledRejection', (reason, p) => {
console.error(reason, 'Unhandled Rejection at Promise', p);
console.log(reason, 'Unhandled Rejection at Promise', p);
process.exit(1);
})
.on('uncaughtException', _err => {
console.error(_err, 'Uncaught Exception thrown');
console.log(_err, 'Uncaught Exception thrown');
process.exit(1);
});

这导致前一个方法在实际将错误写入文件之前就终止了(由于 process.exit(1))。

最佳答案

我发现我实际上捕获了两次 uncaughtExceptions...

process
.on('unhandledRejection', (reason, p) => {
console.error(reason, 'Unhandled Rejection at Promise', p);
console.log(reason, 'Unhandled Rejection at Promise', p);
process.exit(1);
})
.on('uncaughtException', _err => {
console.error(_err, 'Uncaught Exception thrown');
console.log(_err, 'Uncaught Exception thrown');
process.exit(1);
});

这导致前一个方法在实际将错误写入文件之前就终止了(由于 process.exit(1))。

关于node.js fs.createWriteStream 创建文件,但无法无错误地写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50991564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com