I have a Node application, within which I am using Graphics Magick to do some image/pdf manipulation.
I have the following code which calls mosaic() to combine a pdf and png. If I export the result as a png then the process is successful. However if I try to export the result as a pdf then the resulting pdf file does have a size, but opening it shows that there is nothing to see, it appears to be blank. No errors are thrown.
var newFileName = "result.pdf";
gm()
.in('-page', '+0+0')
.in('C:\\Code\\ProjectName\\src\\api\\test\\TestTemplatePDF.pdf')
.in('-page', '+103+70')
.in('C:\\Code\\ProjectName\\src\\api\\test\\pic1.png')
.mosaic()
.stream('pdf', (err, stdout, stderr) => {
if (err) console.log('stream error', err);
console.log('stream');
var writeStream = fs.createWriteStream('./etc/' + newFileName);
stdout.pipe(writeStream);
stderr.on('end', () => {
fs.readFile('./etc/streamError.txt', (err, data) => {
console.log('reading errorStream');
// if (err) console.error(err);
if (err) {
console.log('We found an error reading streamError.txt', err);
res.send(err);
} else if (data.length !== 0) {
console.log('streamError.txt should contain a detailed error message', data);
res.send(data);
} else {
console.log('streamError.txt contains no errors');
}
});
});
stdout.on('end', () => {
fs.readFile('./etc/' + newFileName, (err, data) => {
if (err) {
console.log("stdout error: " + err);
res.end();
} else {
console.log('Successfully read our new image file');
}
})
})
})
Output/console shows:
stream
reading errorStream
streamError.txt contains no errors
successfully read our new file
In the end this problem went away when I converted the pdf to a png before editing.
Presumably the conclusion is that when using mosaic() that they need to be the same type.
Related
I'm attempting to stream images and videos from my S3 bucket to my browser by piping the stream through express. I've got the stream working, but even though it works in the browser I'm still getting NoSuchKey errors every time I make the request.
app.get("/:key", async (req, res) => {
s3.getObject({Bucket: "my-bucket", Key: req.params.key}, (err, data) => {
if (err){
console.log("error: ", err)
}
else {
console.log("data: ", data)
res.set('Content-Type', data.ContentType)
res.set('Content-Length', data.ContentLength)
res.status(200)
let rs = require('stream').Readable.from(data.Body)
rs.pipe(res)
}
})
// try {
// s3.getObject({Bucket: "my-bucket", Key: req.params.key}).promise().then(
// (data) => {
// console.log("data: ", data)
// res.set('Content-Type', data.ContentType)
// res.set('Content-Length', data.ContentLength)
// res.status(200)
// let rs = require('stream').Readable.from(data.Body)
// rs.pipe(res)
// },
// (err) => {
// console.log("error: ", err)
// }
// )
// }
// catch(err){
// console.log(err)
// }
})
I've tried using promises with the commented out code with the same result.
output:
error: NoSuchKey: The specified key does not exist.
at Request.extractError (C:\my_path\node_modules\aws-sdk\lib\services\s3.js:710:35)
...
I would like to send "Hello world" from one nodejs server to another using node-serialport. I have verified that the radios connecting the two are connected and sending info because they keep displaying buffer information after running my current code.
here is what I have so far.
server1
// Import dependencies
const SerialPort = require("serialport");
const Readline = require("#serialport/parser-readline");
var sf = require('sf');
//SerialPort.list(function (err, results) {
// if (err) {
// throw err;
// }
SerialPort.list().then(ports => {
ports.forEach(function(port) {
console.log(port.path);
console.log(port.pnpId);
console.log(port.manufacturer);
});
});
// Defining the serial port
const port = new SerialPort('COM3',{baudRate: 9600}, function (err) {
if (err) {
return console.log('Port Error: ', err.message)
}
})
port.write('main screen turn on', function(err) {
if (err) {
return console.log('Error on write: ', err.message)
}
console.log('message written')
})
// Read data that is available but keep the stream in "paused mode"
port.on('readable', function () {
console.log('Data:', port.read())
})
// Switches the port into "flowing mode"
port.on('data', function (data) {
console.log('Data:', data)
})
// Pipe the data into another stream (like a parser or standard out)
const lineStream = port.pipe(new Readline())
lineStream.on('data', console.log)
server 2
// Import dependencies
// in Ubuntu need to run command: sudo chmod 666 /dev/ttyS0 to open port for use
const SerialPort = require("serialport");
const Readline = require("#serialport/parser-readline");
var stoploop = true;
// Defining the serial port
const port = new SerialPort('/dev/ttyUSB0', function (err) {
if (err) {
return console.log('Error: ', err.message)
}
})
port.write('chicken butt', function(err) {
if (err) {
return console.log('Error on write: ', err.message)
}
console.log('message written')
})
// port.write("hello?");
// Read data that is available but keep the stream in "paused mode"
port.on('readable', function () {
console.log('Data:', port.read())
})
// Switches the port into "flowing mode"
port.on('data', function (data) {
console.log('Data:', data)
})
// Pipe the data into another stream (like a parser or standard out)
const lineStream = port.pipe(new Readline())
any help or even an example of how to send hello world between the two would be greatly appreciated! please let me know if any more info is needed.
edit : I recently tried doing something like
port.on('data', (data) => {
try {
console.log(data.toString());
} catch (err) {
console.log('Oops');
}
});
this is taking data that used to appear as <buffer # # # # #> and turning it into an odd string like "(
)))) ) ) )))
!)☺)!))) ) )
)(☺!�"
I found the answer myself!
I was using the wrong baudRate, and also needed to stringify the data being sent as a JSON string
How to read the data of a file(say, read.txt) without getting the buffered Data?
I mean I want to get the content of the file directly without getting the buffer data?
and hoe to delete the folder in Node.js?
// Try this //
fs = require('fs')
fs.readFile('/read.txt', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
console.log(data);
});
try {
fs.rmdirSync(dir, { recursive: true });
console.log(`${dir} is deleted!`);
}catch (err) {
console.error(`Error while deleting ${dir}.`);
}
I am reading below data from file now i want to create json object from it,how can i create that using nodejs fs module ?
app.js
var path = "./ditLogs/" + file;
fs.readFile(path, function(err, data) {
console.log('reading file data', data.toString());
});
Here is data in file that i need to create json for
file.txt
{"event":"test data"}
{"event":"test data"}
{"event":"test data"}
You can use this sample function:
function(strFromFile) {
try {
return JSON.parse(strFromFile);
} catch(e) {
return {};
}
};
FS Module
While developing in NodeJS the need to access a computer's file system is sometimes necessary. FS Module is a utility that assists with this process.
FS includes some functions that allow for different activities to be done on the file system via a wrapper around the API.
This should be included/required/imported into any JS that needs to interact with the file system API.
var fs = require("fs");
These are the different methods you can use on this API which are all asynchronous:
fs.readFile {fs.readFile('input.txt', function(err, data)
// Asynchronous read
fs.readFile('input.txt', function (err, data) {
if (err) {
return console.error(err);
}
console.log("Asynchronous read: " + data.toString());
});
fs.writeFile
fs.writeFile('input.txt', 'Simply Easy Learning!', function(err) {
if (err) {
return console.error(err);
}
console.log("Data written successfully!");
console.log("Let's read newly written data");
fs.readFile('input.txt', function (err, data) {
if (err) {
return console.error(err);
}
console.log("Asynchronous read: " + data.toString());
});
});
open
// Asynchronous - Opening File
console.log("Going to open file!");
fs.open('input.txt', 'r+', function(err, fd) {
if (err) {
return console.error(err);
}
console.log("File opened successfully!");
});
fs.stat (provides information about the file) e.g.
fs.stat('input.txt', function (err, stats) {
if (err) {
return console.error(err);
}
console.log(stats);
console.log("Got file info successfully!");
// Check file type
console.log("isFile ? " + stats.isFile());
console.log("isDirectory ? " + stats.isDirectory());
});
fs.read (similar to readFile and should not be the first choice for reading a file)
fs.close
// Close the opened file.
fs.close(fd, function(err){
if (err){
console.log(err);
}
console.log("File closed successfully.");
});
ftruncate (truncate an opened file)
unlink (delete an opened file)
fs.unlink('input.txt', function(err) {
if (err) {
return console.error(err);
}
console.log("File deleted successfully!");
});
fs.mkdir (make new directory)
fs.mkdir('/tmp/test',function(err){
if (err) {
return console.error(err);
}
console.log("Directory created successfully!");
});
fs.readdir (reads a directory)
fs.readdir("/tmp/",function(err, files){
if (err) {
return console.error(err);
}
files.forEach( function (file){
console.log( file );
});
});
fs.rmdir (remove directory)
fs.rmdir("/tmp/test",function(err){
if (err) {
return console.error(err);
}
console.log("Going to read directory /tmp");
fs.readdir("/tmp/",function(err, files){
if (err) {
return console.error(err);
}
files.forEach( function (file){
console.log( file );
});
});
});
Synchronous functions:
readFileSync
// Synchronous read
var data = fs.readFileSync('input.txt');
console.log("Synchronous read: " + data.toString());
writeFileSync
// Synchronous write
var data = fs.writeFileSync('input.txt', 'asdasdasd');
Simply read using line-by-line package, less headache, more control on reading process (it can pause, resume reading, close file descriptor ondemand, skip N lines) with less code.
1) install:
npm i --save line-by-line
npm i --save lodash
2) implement
var lineByLine = require('line-by-line'),
_ = require('lodash'),
path = require('path');
var lines = [];
var filePath = path.join(__dirname, "ditLogs", file);
var fileReader = new lineByLine(filePath);
fileReader.on('line', function(line) {
line = JSON.parse(line);
if(_.isPlainObject(line) && !_.isEmpty(line)) {
lines.push(line);
}
// optional
doSomethingWithLine(line);
});
function doSomethingWithLine(line) {
// for example You can save to db or send to somewhere using request libary or just show in console
}
fileReader.on('error', function(error) {
console.error(error);
process.exit(-1);
});
fileReader.on('end', function() {
doSomethingAfterParsingAllLines(lines);
});
function doSomethingAfterParsingAllLines(records) {
// do something with data
}
'use strict';
const fs = require('fs');
let rawdata = fs.readFileSync('student.json');
let student = JSON.parse(rawdata);
console.log(student);
I am trying to use an upload program to upload my files. The code that I use is
app.post('/photos',loadUser, function(req, res) {
var post = new Post();
req.form.complete(function(err, fields, files) {
if(err) {
console.log(err);
next(err);
} else {
ins = fs.createReadStream(files.file.path);
ous = fs.createWriteStream(__dirname + '/public/uploads/photos/' + files.file.filename);
post.filename=files.file.filename;
post.file=files.file.path;
util.pump(ins, ous, function(err) {
if(err) {
next(err);
} else {
post.save(function(err,docs) {
req.flash('info', 'information Saved');
res.redirect('/photos');
});
}
});
}
});
});
When I remove loadUser method everything is working fine, but when I use the loadUser method it is giving me an error. The console information of the error is:
Error: parser error, 0 of 4344 bytes parsed
at IncomingForm.write (/home/darhamid/node_modules/formidable/lib/incoming_form.js:141:17)
at IncomingMessage.<anonymous> (/home/darhamid/node_modules/formidable/lib/incoming_form.js:91:12)
at IncomingMessage.emit (events.js:67:17)
at HTTPParser.onBody (http.js:121:23)
at Socket.ondata (http.js:1349:22)
at TCP.onread (net_uv.js:312:27)
The error is caused only when i use loadUser function, if i remove the loadUser Funciton everything is working fine.
I don't know the reason behind this and am stuck. Can anyone help me please?
See this github issue : https://github.com/felixge/node-formidable/issues/34
Another possible cause for the problem is in this line:
request.setEncoding( "utf8" );
You are trying to perform database operation before everything, which is creating problems for you. Try the following code:
app.post('/potos', function(req, res, next) {
//req.form.pause();
req.form.complete(function(err, fields, files) {
if(err) {
next(err);
} else {
ins = fs.createReadStream(files.file.path);
ous = fs.createWriteStream(__dirname + '/public/uploads/photos/' + files.file.filename);
var post = new Post();
post.filename=files.file.filename;
post.file=files.file.path;
post.created_at = new Date();
post.user_id = req.session.user_id;
function postCreationFailed() {
req.flash('error', 'Unable to Download ');
res.render('photos/new', {
locals: {
post: new Post(),currentUser: req.session.user_id
}
});
}
util.pump(ins, ous, function(err) {
if(err) {
next(err);
} else {
console.log('\nuploaded %s to %s', files.file.filename, files.file.path);
post.save(function(err) {
if (err)
return postCreationFailed();
req.flash('info', 'photos Succesfully Uploaded');
res.redirect('/user/photos/'+post.user_id);
});
}
});
}
});
req.form.on('progress', function(bytesReceived, bytesExpected){
var percent = (bytesReceived / bytesExpected * 100) | 0;
process.stdout.write('Uploading: %' + percent + '\r');
});
});
Good luck...