我有两个节点文件,我需要知道如何打开第二个文件作为第一个文件的URL作为参数
服务器文件(第一个文件)
var express = require('express');
var request = require('request');
var app = express();
var arr2 = [];
app.get('/', function(req, res,body){
var queryParam = req.query;
var url = queryParam.website;
})
app.listen('3000')
console.log('Web Scrape happens on port 3000');
exports = module.exports = app;
(第二个文件)用于从网站提取链接的文件
var request = require('request');
var cheerio = require('cheerio');
request(url , function(err,resp,body){
$ = cheerio.load(body);
links = $('a');
$(links).each(function(i,link){
console.log(url+$(link).attr('href'));
}
)
})
问题在于我如何将URL从第一个文件传递到第二个文件,是否有可能在节点js中将ur作为参数传递?
答案 0 :(得分:1)
尝试一下
(第二个文件)
var request = require('request');
var cheerio = require('cheerio');
module.exports = {
request : function(url , function(err,resp,body){
$ = cheerio.load(body);
links = $('a');
$(links).each(function(i,link){
console.log(url+$(link).attr('href'));
}
)
})
}
第一个文件
var express = require('express');
var request = require('request');
var app = express();
var file2 = require('./file2'); //import file2
var arr2 = [];
app.get('/', function(req, res,body){
var queryParam = req.query;
var url = queryParam.website;
file2.request(url,....) ///fill what you need
})
app.listen('3000')
console.log('Web Scrape happens on port 3000');
exports = module.exports = app;