how about node.js change its api? - node.js

I'm using node.js to make a http request to a host. I found the latest version of node.js of 0.6.7 that make a request is like this
var http = require('http');
var req = http.request(options, function(res) {
var result ='';
res.on('data', function (chunk) {
//console.log('BODY: ' + chunk);
result += chunk;
});
res.on('end', function () {
console.log('end:' + result);
});
});
But I also found that the 0.3 version of node.js that make a request like this
http.createClient(80, 'api.t.sina.com.cn')
.request('GET', '/statuses/public_timeline.json?source=3243248798', {'host': 'api.t.sina.com.cn'})
.addListener('response', function(response){
var result = ''
response.addListener('data',function(data){
result += data
})
.addListener('end',function(){
tweets = JSON.parse(result)
})
})
But the 0.3 version of 'createClient' api is deprecated in 0.6.7. And I don't find any docs that describe this.
I'm worrying about the api will change in the future. That will make my code not run in the future.
Can anyone give me some advice? Thanks!

The API has and will change. That's the caveat for using bleeding-edge tech, like Node. The only advice I can give is to update your code to use the latest API, after all it's not changing that often, or just stick to a working version if you don't have the resources to keep upgrading your code base.. I personally keep my Node projects pretty small, that way upgrading is easier.
Also the use of NPM modules and generally modular code is encouraged. That way some API changes are easy update. Hope this helps. :)

Node 0.3 is astonishingly ancient, and Node moves remarkably fast.
The Node API will change, you need to constantly keep your platform up to date. While this can be annoying, it's also one of the best parts of Node. It keeps people in the habit of upgrading, allowing the Node platform and ecosystem to continually improve without the stifling hassles of backwards compatibility.
If you want a less frequently changing API, perhaps PHP or Java is more suitable for you. There's also nothing stopping you from continuing to use the older versions of Node. :D

Related

Upgrading NodeJS App

I hope this is not too much to ask.
Im new to this NodeJS tech, which I think is amazing, but Im not a really good programmer, I found this awesome example http://softwareas.com/video-sync-with-websocket-and-node/, but I cant make it work. I believe the code its outdated, and Im running the latest Node, I've been trying to fix it but I cant get near close.
I dont fully understand how websockets work, and is giving me a headache.
When I run the server it runs just fine.
var sys = require("util");
var ws = require('websocket-server');
var userCount = [];
var server = ws.createServer({
debug: true
});
server.addListener("connection", function(conn){
server.broadcast("userCount " + ++userCount);
conn.addListener("message", function(message){
server.broadcast(message);
});
});
server.addListener("close", function(conn){
server.broadcast("userCount " + --userCount);
});
server.listen(8000, "localhost");
function log(msg) {
sys.puts(+new Date + ' - ' + msg.toString());
};
But when I interact with the client (Log as a user) the server crashes and I get this error in the console, and I've no idea what it means.
http://i.stack.imgur.com/cLlga.png
I'm really new to this, and I don't understand D:, any help is really appreciated
That package 'websocket-server' is unmaintained for 4 years, you should consider using another one.
There have been several forks in github, https://github.com/miksago/node-websocket-server/network, but with few changes more, so they are almost same obsolete.
Trying the examples in the package, https://github.com/miksago/node-websocket-server/tree/master/test/manual, the echo-server.js (very similar to your code) doesn't work, server hangs (or appears to hang), with no traces. If you try chat-server.js it does work, but I don't know if it does what you need/want.
You can run those examples with command:
node test/manual/chat-server.js
Double check if there is a newer module that does the same :)

synchronous sqlite transactions node

I installed sqlite3 with npm
npm install sqlite3 --save
I have written some basic functions that I would like to be performed synchronously rather then with async.
for instance I would like to get column names in one function and the row count in another.
I would like to simply return these values. currently I am using a callback like so
d.cinfo = function(table, callback){
var o = {};
db.each("PRAGMA table_info(" + table + ")", function(err, col){
o[col.name] = col.type;
}, function(){
if(typeof callback == 'function') callback(o);
});
}
d is an object which is later exposed
however Id like to return the values
d.cinfo = function(table, callback){
var o = {};
db.each("PRAGMA table_info(" + table + ")", function(err, col){
o[col.name] = col.type;
}, function(){
return o;
});
}
is there a way I can achieve this. I found documentation saying it was possible but then I learned that it was outdated and im not sure its meant for the same api
I have implemented bluebird however Promise.promisifyAll(middleware) returns and error "Object # has no method .then() anyone know what im doing wrong
I've come across a problem like this and many others while using Express and it really drives me crazy where I just can't avoid callback hells.
Some of the possible solutions can be:
Using the same old event driven style. Instead of calling nested functions, emit events for them. But I'm assuming you wouldn't like that very much, but that atleast gets rid of the nested callbacks.
There's a npm module called bluebird that allows you to extend existing modules with promises. (Using promisifyAll). So promises will still trim down the code and you'll have much cleaner code than you had before using promises.
If you don't mind switching frameworks, you can try KoaJS that allows you to yield(fun stuff from ECS 6) functions using generator functions(another cool feature). You can have more reading about this here.
So the above code can be re-written like:
var rows = yield db.each() //or something like that
this is an amazing feature but the drawback is that you have to use unstable versions of node (0.11.x). We also faced some issues setting up https using 0.11.x and had to downgrade to 0.10.x (we're not using koa either)
If there are only a few places where you need to do such kind of stuff, I would recommend using bluebird, but if you're tired of having lots of callbacks in your code, better go for Koa, although I've already mentioned you the tradeoffs.

is it possible to "yield" a async process in node v10.x?

i'm using the request module to fetch image from website and pipe it into local file, the code is like:
url = http:/xxx.com/x.jpg;
if(url){
request(url).pipe(localFilePath);
}
if(xxx){
// save the localFilePath to db;
redirect('/index');
}
// The question is the filePath is needed in the index page, so if the file has not downloaded yet, then it can not show the file on index page.
i tried.
request(url).pipe(...).on('end',function(){
....
});
but it seems does't work..
so, i wonder how to do like :
yield xxxxx in node v0.11.x to pause the process until the file is already downloaded completely?
thanks
Yield is only presently available in Node 0.11 (when using the –harmony flag), but this is an unstable release that is probably not suitable for any kind of production use. Node 0.12 shouldn’t be too far away though, as 0.11 has been in development for a while now. So the good news is generators will be available in a stable Node.js release near you very soon!
If you want to stick with 0.10.x you will have to use callbacks or promises for now.

Which nodejs library should I use to write into HDFS?

I have a nodejs application and I want to write data into hadoop HDFS file system. I have seen two main nodejs libraries that can do it: node-hdfs and node-webhdfs. Someone have tried it? Any hints? Which one should I use in production?
I am inclined to use node-webhdfs since it uses WebHDFS REST API. node-hdfs seem to be a c++ binding.
Any help will be greatly appreciated.
You may want to check out webhdfs library. It provides nice and straightforward (similar to fs module API) interface for WebHDFS REST API calls.
Writing to the remote file:
var WebHDFS = require('webhdfs');
var hdfs = WebHDFS.createClient();
var localFileStream = fs.createReadStream('/path/to/local/file');
var remoteFileStream = hdfs.createWriteStream('/path/to/remote/file');
localFileStream.pipe(remoteFileStream);
remoteFileStream.on('error', function onError (err) {
// Do something with the error
});
remoteFileStream.on('finish', function onFinish () {
// Upload is done
});
Reading from the remote file:
var WebHDFS = require('webhdfs');
var hdfs = WebHDFS.createClient();
var remoteFileStream = hdfs.createReadStream('/path/to/remote/file');
remoteFileStream.on('error', function onError (err) {
// Do something with the error
});
remoteFileStream.on('data', function onChunk (chunk) {
// Do something with the data chunk
});
remoteFileStream.on('finish', function onFinish () {
// Upload is done
});
Not good news!!!
Do not use node-hdfs. Although it seems promising, it is now two years obsolete. I've tried to compile it but it does not match the symbols of current libhdfs. If you want to use something like that you'll have to make your own nodejs binding.
You can use node-webhdfs but IMHO there's not much advantage on that. It is better to use an http nodejs lib to make your own requests. The hardest part here is try to hold the very async nature of nodejs, since you might want first to create a folder, and then after successfully create it, create a file and then, at last, write or append data. Everything through http requests that you must send and wait the for answer to then go on....
At least node-webhdfs might be a good reference to you take a look and start your own code.
Br,
Fabio Moreira

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