Tag: 写入文件

循环通过条形码canvas创build缓冲区并写入文件

为了解决这个问题,我正在用一些基本的代码来解决这个问题。 这只是testing一个更大的项目的一些function,我试图find一种方法来接受一个数组填充canvas对象的函数。 然后我也需要循环这些canvas对象,并创build一个缓冲区来写入一个文件。 由于某种原因,当我运行这个如何,我认为我应该asynchronous,它只能保存最后的条码多次。 对我来说,我的saveBarcode()函数似乎是一个错误,其中toBuffer和writeFile函数运行asynchronous,但是它们在toBuffer和writeFile函数中失去了循环im的迭代值。 我似乎找不到一个更好的方式来保存所有这些条形码canvas'文件。 任何input将不胜感激! 如果你需要更多的细节,我会尽我所能回答一切。 这是整个代码: const JsBarcode = require("jsbarcode"); const Canvas = require("canvas"); const fs = require("fs"); //makeid creates a Random 12 Digit # to represent the barcode number, It returns an Array of barcodes based on the amount inputted let makeid = (amount) => { let barcodeArray = []; let […]

Javascript的Node.js完全覆盖文件

我有一个应用程序需要一个data.json文件,以绘制一个D3图。 不过,我需要在onClick -Event上更新该文件: d3.select("#updatebutton").on("click", function(e) { try{ $.get('https://localhost:4444/data', function(data) { }); } catch (e) { alert('Error: ' + e); } }); 以上是带有jquery调用的update-Button。 在我的app.js文件中,我使用它是这样的: app.get('/data', function(req, res, next) { try{ getJSON(); } catch(e) { alert('Error'); } }); getJSON()函数通过https-Request接收数据,处理该数据并将其保存到data.json : function getJSON() { var req = https.get(options, function(response) { // handle the response var res_data = ''; […]

在node.js退出之前写入.txt文件

我希望我的服务器在节点退出并在下次加载时保存一些基本数据。 我试着回答这个问题的build议,但服务器似乎closures,才能写入文件。 这是我正在尝试的确切代码: process.stdin.resume(); function exitHandler(options, err) { if(!serverUp){return;} serverUp = false; fs.writeFile('server.txt', String(search_index),function (err) { console.log("debug") // <– This doesn't happen process.exit(); }); } //do something when app is closing process.on('exit', exitHandler.bind(null,{cleanup:true})); //catches ctrl+c event process.on('SIGINT', exitHandler.bind(null, {exit:true})); //catches uncaught exceptions process.on('uncaughtException', exitHandler.bind(null, {exit:true}));

fs.writeFile不覆盖文件

我正在使用node-crontab来运行脚​​本。 fs.writeFile覆盖第一次循环运行,但之后是追加数据。 我已经尝试在写入之前删除文件,但是正在做同样的事情,第一次删除它,但在随后的运行中开始追加。 我该怎么办? 这是脚本:我ommited一些环境variables… var jobId = crontab.scheduleJob('* * * * *', function() { //Gettting system date and adding leading zeros when are single digits. I need this to build the get request with date filters. var d = new Date(); var nday = d.getDate(); var nmonth = d.getMonth(); var nhour = d.getHours(); var […]