Node.js learning guide or study plan - node.js

I am a PHP developer trying her hands on node.js. The introductory books about it and online tutorials are great and are helping me come to speed with this language
however I find the official site nodejs.org documentation hard to drill through for somewhat reasons I don't understand, may be I am just spoiled by php.net.
For example many asynchronous functions take a callback function with arguments and docs document that fact but the types of arguments these callbacks take are barely (not) documented
See below
fs.readFile('./template.html', function(err, data) {
if (err) {
console.error(err);
res.end('Server Error');
}
else {
var tmpl = data.toString();
}
});
Here the data argument appears to be an object with toString method and that's all I know about it.
Old langange users can you please point or guide me on how to get the most out of this nice langange, it can be anything such as how to read the docs.
Thank you.

The docs for node.js seem clear to me.
The callback is passed two arguments (err, data), where data is the contents of the file.

Related

pgtypes.fetcher is not a function

I have been trying to implement pg-postgis-types npm package in my express project for my internship. I'm using PostgreSQL and Sequelize.
Unfortunately, although I have implemented the code in the documentation, our API returns pgtypes.fetcher is not a function. Does anyone encounter with this issue? I checked the definition of the package in node modules folder and I found the definition, as it should be.
To be a reference, my code is like below.
const getgeojson = async (mapID) => {
try {
postgis(pgtypes.fetcher(pg, connection), null, (err, oids) => {
if (err) {
throw err;
}; ...
I know this is not a much popular repo but maybe someone encounter and solved it before, I just wanted to ask. Sorry if this is a bad question :)
The package is much older, thus, the usage is slightly different from the original documentation in both npm and GitHub repo. You can call functions by changing postgisto postgis.default and pgtypes.fetcher to pgtypes.default.fetcher. This solved my stated issue above. Have a nice day y'all.

How do I post file to Apache Solr using Nodejs?

I am kind of asking about best practice here, since I've looked everywhere but can't find any. I'm creating a Solr-based application using NodeJS, particularly solr-client to connect to the Solr. My application is supossed to be able to receive files in multiple kind of formats (from json, xml, csv, etc, which is possible in Solr) and I am using NodeJS for that.
On terminal I can use command like bin/solr post -c core-name path_to_file to post any kind of format readable by Solr, but I have some trouble doing that in Javascript. The following code just doesn't work.
client.add(thisData, function(err, obj){
if(err){
console.log("Failed add file");
console.log(err.stack);
// throw err;
}else{
// console.log(obj);
console.log("Successful");
}
});
client.commit(function(err, res){
if(err){
console.log(err);
}
if(res){
console.log(res);
}
});
As a note, thisData is a variable containing the file content that I have obtained using fileSystem. I don't understand why I can post any kind of file format from terminal, but can't do the same from programming thing. Is it not allowed in Solr?
Some friends suggested me to convert all the file contents (from xls, csv, json, etc) to json then send the json to Solr. It works, but I realize that it's so much works. I have to use a lot of nodejs packages to convert those files and I don't find them really efficient. I have been looking for the best way to do this on github but seems like I found nothing to reference to. Can someone give me some hints? I am sorry if I am not being really clear, I will explain more if you need me to.

How to use node.js methods in Jade template within Wintersmith

I have asked google like million times and haven't got any concrete answers... I would like to use, say, following code in the jade template... however it gives an error on the first line... Please could you point me to the right direction... Thanks in advance!!
var fs = require('fs');
fs.unlink('/tmp/hello', function (err) {
if (err) throw err;
console.log('successfully deleted /tmp/hello');
});
First, you're missing hyphens to escape your template code to JavaScript. Add a hyphen to each line for starters.
- var something = 0; // example!
Source: Jade reference
Second, #barry-johnson is right to point out your Node code probably belongs somewhere else. It looks like you're trying to clean up a directory during the build. There are other, better ways to accomplish this. I suggest taking a look at the Writing Plugins page for more info.
If that doesn't suit you, please describe in more detail what you're trying to accomplish for better guidance. I hope that helps!

node.js module self talk back to js that requires it

im having problems understanding how to talk 'up and down' between app.js and modules...
I think its with a callback but i've also seen things like self._send(), this.send() and module.exports.emit
I'm quite confused.
I recently installed pdfkit from npm (quite good 6/10 :p) I want to learn by improving it slightly though by adding a done event/callback for doc.write().
I know its not that important but i've been looking through my installed modules and that is probably the easiest example of code that wouldn't hurt to have a 'DONE' I also figured this function would be good to learn from as it uses fs.writeFile which has a function(){} that fires when its finished writing so the fact that i can see where in the code it ends makes it an easy learning tool.
I've modified the code a few times tried to compare modules to see where similar things have been done but i just keep breaking it with errors, i don't feel like i'm getting anywhere:
inside the pdfkit module document.js i've made changes:
var EventEmitter = require('events').EventEmitter;//ben
module.exports = new EventEmitter();//ben
PDFDocument.prototype.write = function(filename, fn, callback) {//ben added callback
return this.output(function(out) {
return fs.writeFile(filename, out, 'binary', fn, function(){//ben added finished function
//module.exports.emit('pdf:saved');//ben
callback();//ben
});
});
};
in my app.js:
doc.write('public_html/img/'+_.c+'_'+_.propertyid+'.pdf',function(){console.log('pdf:saved');});
//doc.on('pdf:saved',function(){console.log('pdf:saved');});
I'm also not really sure what i'm querying on google, please can someone help me?
EventEmitter is required to create objects with event storage, and emit/catch events.
While what you are using in your example is called 'callback'.
It is two different ways, and can be sort of cross used. Sometimes it is good to use events, but sometimes just callback is enough.
The best way to have head around it: play with callbacks (forget about events for now). Try to think of different uses of callbacks and maybe even have callback function and pass it around. Then come to callback chains. And only after start playing with EventEmitter. Remember that EventEmitter is different thing from callbacks, with sometimes compatible use cases, but generally is used in different cases.
Here is your code, simplified with same functionality as you have/need atm:
PDFDocument.prototype.write = function(filename, callback) {
this.output(function(out) {
fs.writeFile(filename, out, 'binary', callback);
});
};
And use it the way you already do.
Do not try to generate garbage of code that will only complicate everything - it is better to study speficic areas seperatelly, and then switch to next one. Otherwise will mess up in mind.
FIXED
fn is the callback function!
PDFDocument.prototype.write = function(filename, fn) {
return this.output(function(out) {
return fs.writeFile(filename, out, 'binary', fn);
});
};
and by naming my callback function to fn it works!
doc.write('public_html/img/'+_.c+'_'+_.propertyid+'.pdf',function fn(){console.log('pdf:saved');});
for me that is a massive learning mountain climbed!!

node-cloudfiles module - Is there a way to track upload progress

If anyone here is familiar with the node-cloudfiles module for node.js, I could use some help in several different areas. Unfortunately, is seems the authors are nearly impossible to reach via their github repo (EDIT: nevermind, someone did reach out to me, I'll send an update when I have an answer of some sort prepared.)
I'll start with my most basic challenge: is there a way to track the progress of the upload? I have tried many things, but the object returned from the .addFile command does not seem to hold any sort of progress stats.
Here is a basic outline of what I am working with.
var readStream = fs.createReadStream(path+'.'+extension, streamopts);
var upOpts = {
headers: {
'content-type': 'video/'+extension,
'content-length': totalBytes
},
remote: CDNfilename,
stream: readStream
};
//reqStream is the object returned from the 'request' module,
//which is used by the 'cloudfiles' module.
var reqStream = cloudClient.addFile(Container.name, upOpts, function (err, uploaded) {
if (err) { console.log(err); }
});
At first I thought I could just use the .bytesWritten property connected to an interval timer, but the object is not a normal node writeStream, so there is no such property.
Charlie (the author of the module) told me that this is possible because it's using a pipe and you just check the data events from the object returned from .addFile, like so:
reqStream.on('data', function () {
/* track progress /*
});
Whenever you need to contact somebody from the nodejitsu team, join the #nodejitsu channel on IRC, they're really active.
At the time of writing this answer, there isn't really a good way to get upload progress for files being sent to cloudfiles. However, one of the nodejitsu geniuses implemented chunked uploading, which in my case, eliminates the need for progress reports. Thanks Bradley.

Resources