For a personal project I'm trying to create a simple oEmbed system using Nodejs.
My route looks like this:
app.get('/oembed/:url', function (req, res) {
oembed.get(req.params.url, function (error, result) {
return res.json(200, {message: "OK"});
});
});
and oembed is exposed using var oembed = require('../oembed');
For the oembed.js itself I have:
var request = require('request');
exports.get = function(url, callback) {
//this bit will be developed to call different functions depending on the URL
html = vimeo(url)
};
function vimeo(url) {
var videoUrl = url;
var endpoint = 'http://www.vimeo.com/api/oembed.json';
var url = endpoint + '?url=' + encodeURIComponent(videoUrl) + '&width=640';
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var video = JSON.parse(body);
return video.html
}
})
}
So far, the vimeo function returns the desired html to the function call but I'm a bit lost from here. I'm not sure how I return the html to the initial request (the oembed.get part) and utilise the callback's error and result parameters.
Any help (and advice) would be much appreciated.
It seems you just don't understand how callback functions work. You need something like this in your oembed.js file:
var request = require('request');
exports.get = function(url, callback) {
var endpoint = 'http://www.vimeo.com/api/oembed.json';
request(endpoint + '?url=' + encodeURIComponent(url) + '&width=640', function (error, response, body) {
if (!error && response.statusCode == 200) {
try {
callback(null, JSON.parse(body).html);
} catch (error) {
callback(error);
}
}
});
};
And your route should look like this:
app.get('/oembed/:url', function (req, res) {
oembed.get(req.params.url, function (error, result) {
if (error) {
res.json(500, {message: error});
} else {
res.json(200, {message: "OK"});
}
});
});
Related
I am making a skill for the Amazon Echo. In my handlers, I have an intent (SelectGardenIntent) that obtains the user_id (needed for following HTTP requests) from the access token successfully, as well as a variable called gardenNumber which is a slot value. To complete the request, I need two other values, the garden_id and the dev_id. I use this gardenNumber and pass it into a function called getGardenId, which will assign the one of the data from the HTTP request to the variable garden_id I have defined in index.js. There are no issues with user_id and gardenNumber. When the function is run, there are no errors from the request, but the callback function with the response is also not executed. The user_id, "about to enter request", and "req done" are correctly logged when tested, but the other log statements in the callback function are not since it is not run. The result is that garden_id is undefined. dev_id is obtained in another method that depends on this garden_id, so dev_id is also undefined. Please help me on this issue. I have pasted the relevant code below.
...
var user_id, garden_id, dev_id;
...
function getGardenId (gardenNumber) {
console.log(user_id);
var path = '/api/rest/client/getgardeninfo?&userid=' + user_id;
var options = {
hostname: server_ip,
port: 80,
path: path,
method: 'GET'
}
console.log("about to enter request");
var req = http.request(options, (res) => {
console.log('entered request');
if (res.statusCode === 200) {
console.log('successful request');
res.setEncoding('utf8');
var body = "";
res.on('data', (chunk) => {
console.log('adding data');
body += chunk.toString();
});
res.on('end', () => {
var obj = JSON.parse(body);
console.log('successfully parsed');
if (obj.error === 200) {
console.log('##gardenid successfully obtained');
garden_id = obj.data[gardenNumber - 1].id;
} else {
console.log("parsing error");
}
});
} else {
console.log("failed request");
}
}); } catch(e) {
console.log("ERROR");
}
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
req.on('finish', () => {
console.log('ended');
})
req.end();
console.log("req done");
}
...
var handlers = {
...
'SelectGardenIntent': function () {
//var filledSlots = delegateSlotCollection.call(this);
var gardenNumber = this.event.request.intent.slots.Garden.value;
user_id = this.event.session.user.accessToken;
getGardenId(gardenNumber);
getDevId(garden_id);
this.emit(':tell', `OK, garden ${gardenNumber} selected, user id is ${user_id}, garden id is ${garden_id}, device id is ${dev_id}`);
}
...
}
You'd better use npm request to make calls.
request.get({
url: 'http://' + server_ip + '/api/rest/client/getgardeninfo?&userid=' + user_id
}, function (err, res, body) {
console.log(body);
})
I am trying to a get request to an API.
This is what I have in Angular
Service:
angular.module('MyApp')
.factory('Search', function($resource) {
return $resource('/api/show/:search');
});
Ctrl:
$scope.$watch('searchStr', function (tmpStr)
{
if (!tmpStr || tmpStr.length == 0)
return 0;
// if searchStr is still the same..
// go ahead and retrieve the data
if (tmpStr === $scope.searchStr) {
console.log(Search);
Search.get({string: $scope.searchStr})
.$promise.then(function(data) {
console.log(data);
$scope.responseData = data;
})
}
});
View:
<input type="text" data-ng-model="searchStr">
<textarea> {{responseData}} </textarea>
Nodejs so far:
app.get('api/show/:search', function(req, res, next) {
request.get('http://thetvdb.com/api/GetSeries.php?seriesname=' + search, function (error, response, body) {
console.log('error', error);
console.log('response', response);
console.log('BODY', body);
});
});
what I need is to get a response from that link above when the user is searching, but all I get is the response from the first get to api/show/:search, what am I missing?
replace search with req.params.search
app.get('api/show/:search', function(req, res, next) {
request.get('http://thetvdb.com/api/GetSeries.php?seriesname=' + req.params.search, function (error, response, body) {
console.log(error, response, body);
});
});
I have made a nodejs script to upload css files to our web application. I've omitted everything that's not necessary:
var downloadCSS = function() {
var download = wget.download(src, output, options);
download.on('end', function(outputMessage) {
var unzipStream = fs.createReadStream(output).pipe(unzip.Extract({ path: path }));
unzipStream.on('close', function() {
findFilesAsync(path);
});
});
}
var findFilesAsync = function(path) {
for (var vendor in searchList) {
(function(filePath, vendor, loginCredentials){
glob(filePath, function(err, files) {
login(loginCredentials, files[0], vendor);
})
})(filePath, vendor, loginCredentials);
}
};
var login = function(credentials, file_local, vendor) {
request.post(
'https://server',
function(error, response, body) {
getSettings(cookie, file_local, vendor);
}
);
};
var getSettings = function(cookie, file_local, vendor) {
request.get(
'https://server',
function(error, response, body) {
fileUpload(cookie, settings, file_local, vendor);
}
);
};
var fileUpload = function(cookie, settings, file_local, vendor) {
var CSS_file = fs.createReadStream(file_local);
CSS_file.on('error', function(err) {
console.log('error at reading CSS file');
});
request.post(
'https://server',
function(error, response, body) {
setSettings(cookie, settings, vendor);
}
);
};
var setSettings = function(cookie, settings, vendor) {
request.put(
'https://server',
function(error, response, body) {
logout(cookie, settings, vendor);
}
);
};
var logout = function(cookie, settings, vendor) {
request.get(
'https://server',
function(error, response, body) {
}
);
};
To upload a css file the functions get called top to bottom if there is no error:
downloadCSS()
findFilesAsync()
login()
getSettings()
fileUpload()
setSettings()
logout()
There are 5 CSS files, so whenever a file is found in the findFilesAsync() function, a new login() function will be called asyncronously.
I know this can be done cleaner with promises, but I don't know where to start?
Take a look at this MDN Promise reference they have given a nice example function for http requests that uses promises. So in order to avoid callbacks you will have to change/modify your request class
İ'm using Node.js , ExpressJS(5.0.0-alpha.1) and request module
This is POST request
app.post('/generateTags', function(req, res) {
var title = req.body.title;
restApiKeywords(title, function(tags) {
console.log(tags);
});
});
});
My callback function;
function restApiKeywords(postTitle,callback){
request_('blabla'+postTitle,{json:true,encoding: "binary"}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = body[1];
if (typeof(callback) === 'function') {callback(data);}
}
});
}
if i call this function in GET method, it is working;
app.get("/test",function(req, res) {
restApiKeywords("Recep Niyaz", function(tags) {
console.log(tags);
});
});
How can i run this fuction into POST request basically ?
FİXED:it is my stupid issue , i created two same post method on same project , sorry my friends :)
I have a form whose submit button calls
exports.postUpdateProfile = function(req, res, next) {
User.findById(req.user.id, function(err, user) {
if (err) return next(err);
user.email = req.body.email || '';
user.profile.name = req.body.name || '';
//check req.body.rescueTimeKey by making http request,
//and parsing response to ensure the key is good
user.profile.rescueTimeKey = req.body.rescueTimeKey;
user.save(function(err) {
if (err) return next(err);
req.flash('success', { msg: 'Profile information updated.' });
res.redirect('/account');
});
});
};
I want to make sure the req.body.rescueTimeKey is valid before saving the profile, I've tried to create a module to perform that check...
//rescue-time.js module
var request = require('request');
exports.validKey = function(key){
var options = {
url: 'https://www.rescuetime.com/anapi/data?key=' + key,
json: true
};
var cb = function callback(error, response, body){
if(error || body.error){
//Key is bad, flash message and don't allow save
}
//key is good, save profile
};
request(options, cb);
}
As you might imagine I am not fully grasping the node style of using callbacks when making async calls, any help to re-organize this code is greatly appreciated.
What you will want to do is add an extra argument to your validKey function to accept a callback which is what we will use after the request.
So your rescue-time.js will look something like this:
// rescue-time.js
var request = require('request');
exports.validKey = function(key, cb) {
var options = {
url: 'https://www.rescuetime.com/anapi/data?key=' + key,
json: true
};
request(options, function (error, response, body) {
if(error || body.error){
cb(false)
}
else {
cb(true);
}
});
};``
We're returning a boolean result of true or false if the key is valid.
Inside your controller you will want something like the following:
var rescueTime = require('./path/to/rescue-time.js');
exports.postUpdateProfile = function(req, res, next) {
User.findById(req.user.id, function(err, user) {
if (err) return next(err);
user.email = req.body.email || '';
user.profile.name = req.body.name || '';
//check req.body.rescueTimeKey by making http request,
//and parsing response to ensure the key is good
user.profile.rescueTimeKey = req.body.rescueTimeKey;
// We're sending in a callback function that will have a "valid" result as a second arg
rescueTime.validKey(user.profile.rescueTimeKey, function(valid) {
// check if valid returned true or false and act accordingly
if (!valid) {
req.flash('error', 'invalid rescueTime key');
res.redirect('/account');
}
else {
user.save(function(err) {
if (err) return next(err);
req.flash('success', { msg: 'Profile information updated.' });
res.redirect('/account');
});
}
});
});
};
Keep in mind this code wasn't tested at all, but more of an example on what you need to do to obtain your desired results.