Unable to export variables within function expressjs/requestjs - javascript

My export code
//export.js
var express = require('express'),
fs = require('fs'),
request = require('request'),
cheerio = require('cheerio'),
app = express();
var episode = [],
title = [],
synopsis = [],
reviews = [],
date = [];
exports.showGrab = function(url,response){
request(url,response, function(error, response, html){
var $ = cheerio.load(html),
shows = {bitten:['http://www.tv.com/shows/bitten-2013/episodes/']};
$('.no_toggle._clearfix').eq(1).filter(function(){
var data = $(this);
episode = data.children().first().children().last().text();
exports.episode = episode;
})
})
}
console.log(episode); // => defined as [] not the value given in the function
//import.js
var express = require('express'),
fs = require('fs'),
request = require('request'),
cheerio = require('cheerio'),
app = express(),
server = require('./export');
console.log(server.episode);
server.showGrab('http://www.tv.com/shows/bitten-2013/episodes/');
within the import script using the function server.showGrab works fine but I need access to the variables within the show grab function in my import script. I believe this problem boils down to a scope issue, if I export variables outside a function they work fine but I was under the impression that declaring variables the way I have done would make them global. How can I run this function in the import script whilst still passing it a url and getting back episode to work with?

#Pointy you were right the import script was calling the value before it was defined
//import.js
if (server.episode === undefined){
var test = setInterval(function(){
if (server.episode != undefined){
clearInterval(test);
}
console.log(server.episode);
}, 1000);
}
this does the trick, for some reason using else instead of wrapping an if in an if does not work.

Related

how to use global variable(sharable variables) in Node.js&Express

I have a file called data.js for keeping data as memory database.
var express = require('express');
var app = express();
var aList;
var bList;
var cList;
module.exports = app;
And I want to initialise data when I start the server. So, I implemented init() in app.js
...
var data = require('./data'); // data.js is located in the same folder.
app.set(...);
app.use(...);
...
init();
...
});
fun init(){
console.log("Hello!");
aList = getDumpDataList(10); // I also tried with 'data.aList = getDumpDataList(10);' but didn't work.
console.log(JSON.stringify(aList));
}
fun getDumpDataList(n){
var list;
... // for loop to generate random elements.
return list;
}
module.exports = app;
When I printed with console.log(), Hello! is printed but aList isn't printed but undefined
And I also want to use the data in routers under routes folder.
So, what I did is.
...
var data = require('./data');
route.get("/...", function(req, res, next){
console.log(JSON.stringify(aList));
...
});
But it is also undefined.
I am just making simple test server that initialise data whenever I re-run.
How can I share variables between the js files?
You do not export those vars:
const express = require('express');
const app = express();
let aList;
let bList;
let cList;
module.exports.app = app;
module.exports.aList = aList;
module.exports.bList = bList;
module.exports.cList = cList;
...but i would not put express in data.js, rather put it in app.js.
I would also initialize those vars with initial values in data.js, if the initial data does not depend on something else.
Last but not least: Do not use var anymore, use let and const instead. It is supported since Node 6+ (https://node.green/). I replaced it in the code.

Scraping with NodeJS

I need to extract links from the url in loop , so basically I need to execute another time the function but I don't know how to made this with nodejs.
var request = require('request');
var cheerio = require('cheerio');
var searchTerm = 'baloncesto';
var url = 'http://mismarcadores.com/' + searchTerm;
request(url , function(err,resp,body){
$ = cheerio.load(body);
links = $('a');
$(links).each(function(i,link){
console.log(url+$(link).attr('href'));
}
)
})
My question is about how to extract the links from this array because this code works correctly (This code shows in console the links) but I need to scrape these links.
The result will be scraping the urls inside each.
var request = require('request');
var cheerio = require('cheerio');
var searchTerm = 'baloncesto';
var url = 'http://mismarcadores.com/' + searchTerm;
request(url , function(err,resp,body){
$ = cheerio.load(body)
var allLinks = []
links = $('a');
$(links).each(function(i,link){
console.log(url+$(link).attr('href'))
var currentLink = url+$(link).attr('href')
allLinks.push(currentLink)
if (i == links.length-1){
useLinks(allLinks)
}
}
)
})
function useLinks(allLinks){
console.log(allLinks)
}
If you're asking how to extract the url from the links received from cheerio you're already doing it. If you'd like to use them elsewhere after the request is finished (e.g. for scraping again), then store them in an array and call a function to use the array after you iterate through the last link.
It should look something like this:
let links = $('a').get().map(a => $(a).attr('href'))
I share my solution is like the question but with differents changues.
I don't extract all links only the link thah I pass by url.
var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app = express();
var searchTerm = 'baloncesto';
var url = 'http://mismarcadores.com/' + searchTerm;
var arr2 = [];
app.get('/webscrape', function(req, res,body){
request(url , function(err,resp,body){
var array2 = [];
var array3 = [];
$ = cheerio.load(body);
links = $('a'); //jquery get all hyperlinks
$(links).each(function(i, link){
if($(link).attr('href').includes("baloncesto")){
array2.push($(link).attr('href'));
}
});
const uniqueLinks = new Set([...array2]);
uniqueLinks.forEach((d) => {
const row = []; // a new array for each row of data
row.push(d);
array3.push(row.join()); // by default, join() uses a ','
});
fs.writeFile('raaga_output.json', JSON.stringify(array3, null, 4), function(err){
console.log('File successfully written! - Check your project directory for the raaga_output.json file');
})
res.send('File successfully written! - Check your project directory for the raaga_output.json file');
})
})
app.listen('3000')
console.log('Web Scrape happens on port 3000');
exports = module.exports = app;
Everyone could use this without any problem.

Node Js required file Unexpected token =

I currently have a massive node JS file (3100+ line of code) and I am trying to split it up into multiple files. I have taken one of the functions and a bunch of firebase database references and put them into their own file inside their own class. For some reason the Firebase references give me an error no matter what even when the syntax seams to be okay:
/.../lib/context.js:13
static database = firebase.database()
^
SyntaxError: Unexpected token =
Here is my code for importing the file
// index.js
'use strict'
var ... = ...()
...
var util = require('util')
var fs = require('fs')
var http = require('http')
var firebase = require('firebase')
var Context = require('./lib/context.js')
and here is my code of the imported file:
'use strict'
var util = require('util')
var firebase = require('firebase')
exports.Context = class
{
constructor()
{
}
static database = firebase.database()
static homeworkRef = database.ref("/homework")
static usersRef = database.ref("/users")
static announcementsRef = database.ref("/announcements")
static votingRef = database.ref("/voting")
static feedbackRef = database.ref("/feedback")
static peerRef = database.ref("/peer_review")
Am I requiring the file correctly? Why does this error keep happening? It worked fine while it was in one file.

loopback model access in custom js file

i want to access a model name userRegistration in my custom js file but every time its showing undefined and shows this error
TypeError: Cannot call method 'findOne' of undefined
please Check code
var loopback = require('loopback');
var app = loopback();
var nodemailer = require("nodemailer");
var smtpTransport = require("nodemailer-smtp-transport");
var path=require('path');
var fs=require('fs');
var Handlebars = require('handlebars');
exports.mailToUser=function(req,res,next){
var userNotification = app.models.UserNotification;
var userregister = app.models.UserRegistration;
userregister.findOne({where:{email:email}},function(err,userobj){
if(err){
next()
}
})
}
Thanks
You shouldn't use var app = loopback();
If you want to access to app you can require your server.js or some other ways existed. The simple one is requiring server

Can I know if a javascript file has been executed?

I am using a express framework on node and I want to change the value(increase or decrease) of a variable in my testing module each time the module is run. Is there a way to know if the file has been executed and keep it in memory so that next time the file is run again, the value changes again? I want to increase the variableToChange each time the module is executed.
Here is my code:
'use strict';
var util = require('util');
var makeApiCall = require('proc-utils').makeApiCall;
var baseUrl = 'http://localhost:' + (require('../../config').port || 3000);
var url = {
endpoint: '/patients/register',
method: 'post'
};
var ct = {
test: function (data, cb) {
var options = {
type: 'form',
data: data,
baseUrl: baseUrl
};
makeApiCall(url.endpoint, url.method, options, cb);
}
};
module.exports = ct;
//-- Test Code ----------------------------------------------------------
var variableToChange=0;
if (require.main === module) {
(function () {
var data = {
first_name:'John',
last_name:'Doe',
email:'shnsdfn'+variableToChange+'b#sh.com',
password:'John1234'
};
ct.test(data, console.log);
})();
}
One solution is storing the information you need in the cookie session:
var cookieParser = require('cookie-parser');
var session = require('express-session');
express().use(cookieParser()) // include the cookies middleware to parse and manage sessions
.use(session({secret : 'mysecret'}))//set up the session with minimal config (secret)
.use(function(req,res,next){
req.session.variableToSave = 0; // initialize variable you want to save
next(); // starts next middleware
});
But this is for storing informations for browser that connects to the server.
Other Solution is maintaining the log file by appending the desired values with other relevant informations to it.
Check app.locals in Express API reference: http://expressjs.com/api.html#app.locals

Categories