How to call a function in different nodejs files [closed] - node.js

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i need to call function sendPostData(postData) into fb.js because when i run post.js file i get error name call postData is not defined , postData i defined in fb.js i need to get the postData in the post.js if you check in below post.js i have coded req.write(postData);
how can i call the function ?
file name - fb.js
var https=require('follow-redirects').https;
require('./post');
var args = process.argv.slice(2);
console.log('url:' + args[0]);
var host =args[0].match (/w.*m/)[0];
console.log('host: ' + host);
var path =args[0].match (/(\/d.*)/)[0];
console.log('path: ' + path);
var opti = {
host: host,
port: 443,
path: path,
headers: {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:25.0) Gecko/20100101 Firefox/26.0'}
}
var req=https.request(opti, function (res){
//console.log('Status: ' + res.statusCode);
var data='';
res.on('data',function(chunk){
data += chunk
});
res.on('end',function(){
//console.log('Data: ' + data);
// console.log(data);
var match=data.match(/Directory Results for(.*)\/li>/);
if (!match) {
//console.log('Match not found');
match=data.match(/selected"(.*?)fbD/);
//console.log('Match: ' + match[0]);
}
var postData = '';
var match1=match.toString().match(/<a>|\/directory\/people\/(.*?)">(.*?)<\/a>/g);
match1=match1.removeDuplicates();
for (var index = 0; index < match1.length; index++) {
var link = match1[index];
link = link.substring(link.indexOf('"'),0);
link = 'http://www.facebook.com' + link;
postData += '&url=' + encodeURIComponent(link);
console.log(link);
//sendPostData(postData)=postData;
}
});
});
Array.prototype.removeDuplicates = function (){
var temp=new Array();
this.sort();
for(i=0;i<this.length;i++){
if(this[i]==this[i+1]) {continue}
else{
temp[temp.length]=this[i];
}
}
return temp;
}
req.end();
file name - post.js
var querystring = require('querystring');
var http = require('http');
function sendPostData(postData) {
var options = {
host: 'staging.360social.me',
port: 80,
path: '/queueNewDirectoryPageUrls',
method: 'POST'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.write(postData);
req.end();
}

First, in your post.js
exports.sendPostData=sendPostData;
in your fb.js:
var p = require('./post');
and then call it
p.sendPostData(postData);

Related

Why is this not working, And how do you debug code in nodejs?

First I'm trying to learn nodejs, and for that I am writing like a router. As you have in express.
This is my code:
function sirus() {
var util = utilFuncs(),
paths = {};
this.router = util.handleReq;
this.con = {
get: function (path, fn, options) {
options = (options || {}).method = "GET";
util.makeRequest(path, fn, options);
},
post: function (path, fn, options) {
options = (options || {}).method = "POST";
util.makeRequest(path, fn, options);
}
};
this.req = {
get: function (path, fn) {
util.addPath("GET", path, fn);
},
post: function (path, fn) {
util.addPath("POST", path, fn);
}
};
function utilFuncs() {
function handleReq(req, res) {
var url = parsePath(req.url);
var path = paths[req.method + url];
// console.log(req.url, url +' requested');
if (typeof path != "function") {
res.writeHead(404);
} else {
path(req, res);
}
res.end();
}
function addPath(type, path, callback) {
// add path as a key of object for easier look up, root=/, /a=/a, /a/=/a, /a?=/a, /a/?..=/a so there are no duplicates path=parsePath(path);
paths[type + path] = callback;
}
function parsePath(path) {
path = url.parse(path).pathname;
if ((/[^\/]+\/(?=$)/igm).test(path)) path = path.substring(0, path.length - 1);
return path;
}
function makeRequest(path, fn, options) {
var urls = url.parse(path);
var d = {
host: null,
hostname: null,
method: "GET",
path: '/',
port: 80,
headers: {},
auth: null,
agent: false,
keepAlive: false
};
for (var k in options) d[k] = options[k];
d.host = urls.host;
d.hostname = urls.hostname;
d.path = urls.pathname;
d.headers['Content-Type'] = 'application/x-www-form-urlencoded';
d.headers['Content-Length'] = ((d || {}).body || {}).length || '';
var req = http.request(options, function (res) {
// console.log('STATUS: ' + res.statusCode);
// console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function (chunk) {
data += chunk;
});
res.on('response', function () {
fn(res);
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.write(d.body);
req.end();
}
return {
makeRequest: makeRequest,
handleReq: handleReq,
addPath: addPath
};
}
}
and i use it like this:
var http = require('http'),
url = require('url'),
app = new sirus();
http.createServer(function (req, res) {
app.router(req, res);
}).listen(80, '127.0.0.1');
app.req.get('/', function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
var link = "https://www.reddit.com/";
res.write('Click here');
});
app.con.get('http://www.google.com/', function (res) {
console.log('from req: ' + res);
});
The error i get is The First argument must be string or buffer
Receiving part works correctly, but when i started to add make request part something isn't right.
Also since I'm new to JS and Nodejs. I'd like general advice or anything that catch your eye something that i can be improved about this code. Since I'm note sure i am doing things the best way.
On line 85
req.write(d.body);
there is no member named "body" in the "d" object.
1 - Why is this not working - you get exactly error that tells you why it is not working. If you check more details about error it shows the file and which line error happened at. Perhaps the arguments you pass to function call are not what Express is expecting.
2 - How to debug - look at node.js package called nodev.

Upload a video to youtube with node.js

I've made some headway with the official Google API node client but I've reached a bit of a dead end figuring out how to get my video file up to youtube.
I have a video: example.mp4
I have this code:
OAuth2Client = googleapis.OAuth2Client;
var oauth2Client = new OAuth2Client('XXXXXX', 'XXXXXXX', 'http://callback_url');
googleapis.discover('youtube', 'v3').execute(function(err, client) {
if (err) console.log(err);
// I think the following is the bones of what I want to do
client.youtube.videos.insert({}).execute(function(err, client) {
if (err) console.log(err);
});
});
I only get an error using the insert method (which I expected with no params passed), the client initialises and returns fine.
I'm not sure how to pass the video (in the same directory as the script) to YouTube. Just a pointer would be greatly appreciated.
How about this code:
var googleapis = require('googleapis');
googleapis.discover('youtube', 'v3').execute(function(err, client) {
var metadata = {
snippet: { title:'title', description: 'description'},
status: { privacyStatus: 'private' }
};
client
.youtube.videos.insert({ part: 'snippet,status'}, metadata)
.withMedia('video/mp4', fs.readFileSync('user.flv'))
.withAuthClient(auth)
.execute(function(err, result) {
if (err) console.log(err);
else console.log(JSON.stringify(result, null, ' '));
});
});
You could use this package to do that:
https://github.com/h2non/youtube-video-api
This code works for me:
var file_reader = fs.createReadStream(file_path, {encoding: 'binary'});
var file_contents = '';
file_reader.on('data', function(data)
{
file_contents += data;
});
file_reader.on('end', function()
{
var xml =
'<?xml version="1.0"?>' +
'<entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007">' +
' <media:group>' +
' <media:title type="plain">' + title + '</media:title>' +
' <media:description type="plain">' + description + '</media:description>' +
' <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">' + category + '</media:category>' +
' <media:keywords>' + keywords + '</media:keywords>' +
' </media:group>' +
'</entry>';
var boundary = Math.random();
var post_data = [];
var part = '';
part = "--" + boundary + "\r\nContent-Type: application/atom+xml; charset=UTF-8\r\n\r\n" + xml + "\r\n";
post_data.push(new Buffer(part, "utf8"));
part = "--" + boundary + "\r\nContent-Type: video/mp4\r\nContent-Transfer-Encoding: binary\r\n\r\n";
post_data.push(new Buffer(part, 'ascii'));
post_data.push(new Buffer(file_contents, 'binary'));
post_data.push(new Buffer("\r\n--" + boundary + "--"), 'ascii');
var post_length = 0;
for(var i = 0; i < post_data.length; i++)
{
post_length += post_data[i].length;
}
var options = {
host: 'uploads.gdata.youtube.com',
port: 80,
path: '/feeds/api/users/default/uploads?alt=json',
method: 'POST',
headers: {
'Authorization': 'GoogleLogin auth=' + auth_key,
'GData-Version': '2',
'X-GData-Key': 'key=' + exports.developer_key,
'Slug': 'example.mp4',
'Content-Type': 'multipart/related; boundary="' + boundary + '"',
'Content-Length': post_length,
'Connection': 'close'
}
}
var req = http.request(options, function(res)
{
res.setEncoding('utf8');
var response = '';
res.on('data', function(chunk)
{
response += chunk;
});
res.on('end', function()
{
console.log(response);
response = JSON.parse(response);
callback(response);
});
});
for (var i = 0; i < post_data.length; i++)
{
req.write(post_data[i]);
}
req.on('error', function(e) {
console.error(e);
});
req.end();
});

How would I do this AJAX in Node?

I can write this Instagram API call using jQuery, but I'm not sure how to get Node and/or socket to do the same. Any help?
function getData(url){
$.ajax({
url: url,
dataType :"jsonp",
success: function(data){
console.log('success');
console.log(data);
}
});
}
var tag = "myTag";
var accessToken = "myAccessToken"
var url = 'https://api.instagram.com/v1/tags/' + tag + '/media/recent?access_token=' + accessToken + '&callback=callBackFunction';
getData(url);
Another way to do this would be to use request module
var request = require('request');
request('your url',function(error,response,body){
//body will contain the response from the api endpoint.
});
This should work as you would like it to (adding some extra STATUS, HEADERS and BODY markers) :
var http = require('http');
var myhost = "https://api.instagram.com/";
var tag = "myTag";
var accessToken = "myAccessToken"
var myurl = tag + '/media/recent?access_token=' + accessToken + '&callback=callBackFunction';
var options = {
hostname: myhost,
port: 80,
path: myurl,
method: 'GET'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});

Node.js file download gets truncated by "require" statement

I'm trying to download and require a .js file using a node.js script, but only part of the file is being downloaded.
Specifically, this is the part that seems to be causing the problem:
response.on('data', function (chunk) {
out.write(chunk);
var theModule = require(__dirname + "/" + filename);
//less than half of the file is downloaded when the above line is included.
});
Here is the full source code:
var http = require('http');
var fs = require('fs');
downloadModule("functionChecker.js");
function downloadModule(filename) {
var google = http.createClient(80, 'www.google.com');
var request = google.request('GET', '/svn/' + filename, {
'host': 'javascript-modules.googlecode.com'
});
request.end();
out = fs.createWriteStream(filename);
request.on('response', function (response) {
response.setEncoding('utf8');
response.on('data', function (chunk) {
out.write(chunk);
var theModule = require(__dirname + "/" + filename);
//less than half of the file is downloaded when the above line is included.
//If the import statement is omitted, then the file is downloaded normally.
});
});
}
The data event can be called multiple times. You need to wait until all of the data is written.
response.setEncoding('utf8');
response.on('data', function (chunk) {
out.write(chunk);
});
response.on('end', function(){
out.end();
var theModule = require(__dirname + "/" + filename);
});
Also, createClient is deprecated, as mentioned in the docs. I'd also recommend using pipe to simplify your logic.
function downloadModule(filename) {
http.get({
hostname: 'javascript-modules.googlecode.com',
path: '/svn/' + filename
}, function(res){
var out = fs.createWriteStream(filename);
out.on('close', function(){
var theModule = require(__dirname + "/" + filename);
});
res.pipe(out);
});
}

Node.js YouTube API Upload unable to convert video

I'm trying to upload video to youtube programatically. I chose to use Node.js for the task.
I get an XML response as well as an HTTP Status Code of 201 and I see the video appear in video manager, however the video always has the message "Failed (unable to convert video file)".
I can upload the file through YouTube's own uploader on their page and there are no problems. I only have to upload to a single account, so I set up the OAuth2 for the account and stored the refresh token. The refresh token is hard-coded, though I replace it with a variable below.
Does the refresh token need to, itself, be refreshed?
My code:
var qs = require('querystring'),
https = require('https'),
fs = require('fs');
var p_data = qs.stringify({
client_id: myClientID,
client_secret: myClientSecret,
refresh_token: refreshTokenForAccount,
grant_type: 'refresh_token'
});
var p_options = {
host: 'accounts.google.com',
port: '443',
method: 'POST',
path: '/o/oauth2/token',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': p_data.length,
'X-GData-Key': myDeveloperKey
}
};
var file_path = process.argv[1] || "video.mp4";
var json = "";
var p_req = https.request(p_options, function(resp){
resp.setEncoding( 'utf8' );
resp.on('data', function( chunk ){
json += chunk;
});
resp.on("end", function(){
debugger;
var access_token = JSON.parse(json).access_token;
var title="test upload1",
description="Second attempt at an API video upload",
keywords="",
category="Comedy";
var file_reader = fs.createReadStream(file_path, {encoding: 'binary'});
var file_contents = '';
file_reader.on('data', function(data)
{
file_contents += data;
});
file_reader.on('end', function()
{
var xml =
'<?xml version="1.0"?>' +
'<entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007">' +
' <media:group>' +
' <media:title type="plain">' + title + '</media:title>' +
' <media:description type="plain">' + description + '</media:description>' +
' <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">' + category + '</media:category>' +
' <media:keywords>' + keywords + '</media:keywords>' +
' </media:group>' +
'</entry>';
var boundary = Math.random();
var post_data = [];
var part = '';
part = "--" + boundary + "\r\nContent-Type: application/atom+xml; charset=UTF-8\r\n\r\n" + xml + "\r\n";
post_data.push(new Buffer(part, "utf8"));
part = "--" + boundary + "\r\nContent-Type: video/mp4\r\nContent-Transfer-Encoding: binary\r\n\r\n";
post_data.push(new Buffer(part, 'utf8'));
post_data.push(new Buffer(file_contents, 'binary'));
post_data.push(new Buffer("\r\n--" + boundary + "--\r\n\r\n", 'utf8'));
var post_length = 0;
for(var i = 0; i < post_data.length; i++)
{
post_length += post_data[i].length;
}
var options = {
host: 'uploads.gdata.youtube.com',
port: 443,
path: '/feeds/api/users/default/uploads',
method: 'POST',
headers: {
'Authorization': 'Bearer ' + access_token,
'X-GData-Key': myDeveloperKey,
'Slug': 'video.mp4',
'Content-Type': 'multipart/related; boundary="' + boundary + '"',
'Content-Length': post_length,
'Connection': 'close'
}
}
var req = https.request(options, function(res)
{
res.setEncoding('utf8');
console.dir(res.statusCode);
console.dir(res.headers);
var response = '';
res.on('data', function(chunk)
{
response += chunk;
});
res.on('end', function()
{
console.log( "We got response: " );
console.log(response);
});
});
for (var i = 0; i < post_data.length; i++)
{
req.write(post_data[i]);
}
req.on('error', function(e) {
console.error(e);
});
req.end();
});
});
});
p_req.write(p_data);
p_req.end();
The problem was in the file being uploaded.
This line: var file_path = process.argv[1] || "video.mp4"; should have been var file_path = process.argv[2] || "video.mp4";
Note argv[1] is the absolute path to the script being run, argv[2] is the first command line argument passed to the script.
Of course YouTube would fail to convert the "video", it wasn't video at all it was the script being run.

Resources