我有两个节点文件,我需要知道如何打开第二个文件作为第一个文件的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作为参数传递?

最佳答案

试试这个

(第二个文件)

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;

关于node.js - 有人可以帮我传递URL作为参数吗,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54602403/

10-16 19:22