Node.js:
var https = require("https");
var request = https.get("google.com/", function(response) {
console.log(response.statusCode);
});
request.on("error", function(error) {
console.log(error.message);
});
If I add https:// to the google domain name then I get the status code 200 as expected. As is, I would expect the error to be caught and an error message similar to "connect ECONNREFUSED" to be printed to the terminal console. Instead it prints the stacktrace to the terminal.
If you look at the source for https.get(), you can see that if the parsing of the URL fails (which it will when you only pass it "google.com/" since that isn't a valid URL), then it throws synchronously:
exports.get = function(options, cb) {
var req = exports.request(options, cb);
req.end();
return req;
};
exports.request = function(options, cb) {
if (typeof options === 'string') {
options = url.parse(options);
if (!options.hostname) {
throw new Error('Unable to determine the domain name');
}
} else {
options = util._extend({}, options);
}
options._defaultAgent = globalAgent;
return http.request(options, cb);
};
So, if you want to catch that particular type of error, you need a try/catch around your call to https.get() like this:
var https = require("https");
try {
var request = https.get("google.com/", function(response) {
console.log(response.statusCode);
}).on("error", function(error) {
console.log(error.message);
});
} catch(e) {
console.log(e);
}
Related
I have some http.get code that looks like this
http.get(url, function (response) {
var data = '';
response.on('data', function (x) {
data += x;
});
response.on('end', function () {
var json = JSON.parse(data);
console.log(json);
});
});
How do I error handling this if an invalid URL/API endpoint is provided?
you can handle http.get errors by check state error example:-
http.get(url, function (response) {
response.on('error', function (err) {
//do some thing , error handling
console.log(err);
});
});
you can validate the url by
validating response code & ping domain or url
1 - validate response code :-
http.get(url, function (response) {
if(response.statusCode !== 200){
//error happens
}
});
2- ping on the giving url using this repo valid-url:-
var validUrl = require('valid-url');
if (!validUrl.isUri('apiUrl')){
//error not valid url
}
I'm attempting to post an image onto the twitter api, v1.1
I've tried just about all the example out there, and nothing seems to be able to post it.
include Posting images to twitter in Node.js using Oauth
I'm using the oauth library mentioned there, and I also had jsOauth, which I thought I'd give a shot according to https://gist.github.com/lukaszkorecki/1038408
Nothing has worked, and at this point I'm starting to lose hope on whether I can even do this.
function postStatusWithMedia(status, file) {
var err = new Object();
if(fs.existsSync(file) === false) {
err.message = "File not found :(";
parseTwitterError(err);
} else {
var oauth = OAuth(options = {
"consumerKey": consumer_key,
"consumerSecret": consumer_secret,
"accessTokenKey": access_token,
"accessTokenSecret": access_token_secret
});
callbacks = {
onSuccess : function() {
console.log('upload worked!')
},
onFailure : function() {
console.log('upload failed!');
console.dir(arguments);
}
},
uploadData = {
'status' : status,
'media' : Base64.encode(fs.readFileSync(file))
};
oauth.post('https://api.twitter.com/1.1/statuses/update_with_media.json',uploadData, callbacks.onSuccess, callbacks.onFailure);
return false;
}
}
If it can't be done, can you please explain why?
Otherwise, anything that could lead me to the right direction would be great.
var fs = require('fs');
var request = require('request');
var FormData = require('form-data');
var utf8 = require('utf8');
// Encode in UTF-8
status = utf8.encode(status);
var form = new FormData();
form.append('status', status)
form.append('media[]', fs.createReadStream(file));
// Twitter OAuth
form.getLength(function(err, length){
if (err) {
return requestCallback(err);
}
var oauth = {
consumer_key: consumer_key,
consumer_secret: consumer_secret,
token: access_token,
token_secret: access_token_secret
};
var r = request.post({url:"https://api.twitter.com/1.1/statuses/update_with_media.json", oauth:oauth, host: "api.twitter.com", protocol: "https:"}, requestCallback);
r._form = form;
r.setHeader('content-length', length);
});
function requestCallback(err, res, body) {
if(err) {
throw err;
} else {
console.log("Tweet and Image uploaded successfully!");
}
}
I ended up using request and node-form-data to manually construct a multipart/form-data request and send it with the status request, utf8 was for encoding the status into UTF-8, not doing so caused issues with '<3' and other characters.
I have not tested these code.Its from my colleague.sure the code is working.
Perhaps this will help.
//twitter_update_with_media.js
(function() {
var fs, path, request, twitter_update_with_media;
fs = require('fs');
path = require('path');
request = require('request');
twitter_update_with_media = (function() {
function twitter_update_with_media(auth_settings) {
this.auth_settings = auth_settings;
this.api_url = 'https://api.twitter.com/1.1/statuses/update_with_media.json';
}
twitter_update_with_media.prototype.post = function(status, imageUrl, callback) {
var form, r;
r = request.post(this.api_url, {
oauth: this.auth_settings
}, callback);
form = r.form();
form.append('status', status);
return form.append('media[]', request(imageUrl));
};
return twitter_update_with_media;
})();
module.exports = twitter_update_with_media;
}).call(this);
next file
//upload_to_twitter.js
var tuwm = new twitter_update_with_media({
consumer_key: TWITTER_OAUTH_KEY,
consumer_secret: TWITTER_OAUTH_SECRET,
token: access[0],
token_secret: access[1]
});
media_picture.picture = imageURL;
if (media_picture.picture) {
console.log('with media upload');
request.head(media_picture.picture,
function (error, response, body) {
if (!error && response.statusCode == 200) {
var image_size = response.headers['content-length'];
if (image_size > 2000000) { // 2mb max upload limit
console.log('greater than 2mb');
sendMessageWithoutImage(err, req, res, next, twit, wallpost, access);
} else {
console.log('less than 2mb');
console.log('twitter text', content);
tuwm.post(content, media_picture.picture, function(err, response) {
if (err) {
console.log('error', err);
return next(err);
}
error_parse = JSON.parse(response.body);
console.log('with media response', response.body);
if (error_parse.errors) {
console.log('have errors', error_parse);
res.json({
status: 500,
info: error_parse.errors[0].code + ' ' + error_parse.errors[0].message
});
} else {
res.json({
status: 200,
info: "OK",
id: response.id
});
}
});
}
}
});
I am using this code to download files in node js :
var currentVideoRequest = null;
window.spawnVideoPlayer = function (url, subs, movieModel,tag) {
if(currentVideoRequest) {
try
{
currentVideoRequest.abort();
}
catch(err)
{
alert(err.message);
}
}
var fs = require('fs');
var urlrequest = currentVideoRequest = require('request');
urlrequest.get({url: url, encoding: 'binary'}, function (err, response, body) {
fs.writeFile(FILEURL, body, 'binary', function(err) {
});
});
}
And in the currentVideoRequest.abort(); i get this error:
Object function request(uri, options, callback) {
if (typeof uri === 'undefined') throw new Error('undefined is not a valid uri or options object.')
if ((typeof options === 'function') && !callback) callback = options
if (options && typeof options === 'object') {
options.uri = uri
} else if (typeof uri === 'string') {
options = {uri:uri}
} else {
options = uri
}
options = copy(options)
if (callback) options.callback = callback
var r = new Request(options)
return r
} has no method 'abort'
To add to #Etai's answer, you need to require the request module before using it for one instance of the request. Something like this:
var request = require('request');
// ...
// then later in the code
var urlrequest = request.get(uri, function(err, response, body) {
// process data here
});
// later, you'd abort this as:
urlrequest.abort();
Note that I'm saving the instance with var urlrequest = request.get(params, callback); so that I can call abort on it later.
your currentVideoRequest is a constructor for a request object, not a request object, which is why this is failing.
The request constructor returns a request object when invoked, i.e.
require('request')('uri', function(err, resp, body){})
you can use abort() method to stop that request.
var reqObj = request({uri: 'https://jsonplaceholder.typicode.com/albums' }, function (error, response, body) {
console.log('API requested ') ;
if (!err){
console.log(body);
}
else
{
console.log(err);
}
});
reqObj.abort();
I think you can use this method to get what you need.
I used async in this way, which makes both the code cleaner and less callback .
(async function main() {
try {
let urlRequest = await customRequest("http://127.0.0.1:8000/api/product/search?title=foo");
urlRequest.abort();
} catch (e) {
console.log(e);
}
})();
function customRequest(url) {
return new Promise((resolve, reject) => {
request.get(url, (err, res) => {
if (err) reject(err)
if (res.statusCode !== 200)
reject("my Error ");
resolve(res);
})
})
}
Also, if you do not need to answer the urlRequest variable,you can remove the await from the function as shown below.
try {
let urlRequest = customRequest("http://127.0.0.1:8000/api/product/search?title="); // run in background !
urlRequest.abort();
} catch (e) {
console.log(e);
}
Finally, since our request returns an exception if you make a mistake, you can write abort() in the catch block if needed.
(async function main() {
try {
var urlRequest =await customRequest("http://127.0.0.1:8000/api/product/search?title=");
} catch (e) {
urlRequest.abort();
console.log(e);
}
})();
Also, because the customRequest() function returns a promise note, you use then() instead of async await.
I'm trying to make a simple authentication with node js. Because I read user data from a database, I have to make it asynchronous. Here's my function, which checks if authentication is ok:
function auth(req, callback) {
var header = req.headers['authorization'];
console.log(cb.type);
console.log("Authorization Header is: ", header);
if(!header) {
callback(false);
}
else if(header) {
var tmp = header.split(' ');
var buf = new Buffer(tmp[1], 'base64');
var plain_auth = buf.toString();
console.log("Decoded Authorization ", plain_auth);
var creds = plain_auth.split(':');
var name = creds[0];
var password = creds[1];
User.findOne({name:name, password:password}, function(err, user) {
if (user){
callback(true);
}else {
callback(false);
}
});
}
}
And here I call the function:
auth (req, function (success){
if (!success){
res.setHeader('WWW-Authenticate', 'Basic realm="myRealm');
res.status(401).send("Unauthorized");
}else{
if(user!==req.user) {
res.status(403).send("Unauthorized");
}else{
User.findOneAndUpdate({user:userid}, {user:req.body.user, name:req.body.name, email:req.user.email, password:User.generateHash(req.body.password)},
{upsert:true}, function(err, user) {
if(!err) {
res.status(200).send("OK");
}else{
res.status(400).send("Error");
}
});
}
}
});
This gives me error "TypeError: object is not a function", pointing at "callback(false)". I have no idea what could cause this error, as I pass a function as a parameter, and the first log message prints "[function]". Any help would be appreciated.
In the following code, why does the createFile callback fire twice? This only happens when the server (below) is processing two or more requests at the same time, not if only one request has been made. Output at the bottom of the post. The client making the request is not a browser, but another node.js script iterating through a directory and sending a http post request with the file to the server. The request is created like this:
fs.createReadStream(fileName).pipe(httprequest(options, function(error, response, body) { }));
function myRequest(request, response) {
function writeFile(filePath, request, callback) {
newFilePath = "/home/pi/upload"+filePath; //filePath looks like this: /home/blah/file.txt, the code below creates this structure under another directory, so newFilePath becomes /home/pi/upload/home/blah/file.txt
tempFileName = path.basename(filePath)+".temp";
console.log("Processing "+filePath+"->"+newFilePath+" with tempname " +tempFileName);
var createFile = request.pipe(fs.createWriteStream(tempFileName));
createFile.on("finish", function(error) { //Why does it fire the callback twice?
if(error) {
throw error;
} else {
moveFile(tempFileName, newFilePath, function(error) {
if(error) {
throw error;
} else {
console.log("OK");
}
});
}
});
}
function moveFile(tempFileName, newFilePath, callback) {
dirPath = path.dirname(newFilePath);
fs.stat(dirPath, function(error, stats) { //check if dir exists
if(error == null) {
console.log(dirPath+" already exists");
fs.stat(tempFileName, function(error, stats) { //check if file exists
if(error == null) {
console.log("OK, writing "+newFilePath);
fs.rename(tempFileName, newFilePath, function(error) {
if(error) { //Error on the second run, because the file has been moved in the first run, shouldn't happen?
throw error;
} else {
var myCB = JSON.stringify({fMove: "OK"});
callback(myCB);
}
});
} else {
console.log("File exists");
}
});
}
});
}
writeFile(fileName, request, function() {
//Do some stuff
});
request.on("end", function() {
//Do other stuff
}
});
http.createServer(myRequest).listen(8888);
Output from my script
Processing /home/pi/app/temp/client.js->/home/pi/upload/home/pi/app/temp/client.js with tempname client.js.temp
/home/pi/upload/home/pi/app/temp already exists
/home/pi/upload/home/pi/app/temp already exists
OK, Writing /home/pi/upload/home/pi/app/temp/client.js
OK, Writing /home/pi/upload/home/pi/app/temp/client.js
/home/pi/app/server.js:67
throw error;
^
{"fMove":"OK"}
Incorrect error handling made the script faulty.
In the moveFile function this part was wrong:
fs.rename(tempFileName, newFilePath, function(error) {
if(error) {
throw error;
} else {
var myCB = JSON.stringify({fMove: "OK"});
callback(myCB); // <-- Malformatted callback! Should be callback(null, myCB);
}
Which made this part of writeFile trigger on error and for some reason run twice:
moveFile(tempFileName, newFilePath, function(error) { //Should be moveFile(tempFileName, newFilePath, function(error, status) {
if(error) {
throw error;
} else {
console.log("OK");
}
});
When I fixed my code so it handles the error correctly, it works as intended!