gpt4 book ai didi

node.js - 如何组合两个都需要监听端口的 Express 模块?

转载 作者:太空宇宙 更新时间:2023-11-04 00:49:47 25 4
gpt4 key购买 nike

我正在尝试创建一个网络抓取工具,用户在表单中输入 URL,当他们点击提交时,抓取工具会获取该 URL,然后返回有关我指定的 URL 的数据。

我的主要 app.js 文件是:

// Dependencies
var express = require('express');
var path = require('path');
var fs = require('fs');

// Custom Libraries - ./ signals to node not to look in the node_modules directory
var scraper = require('./scraper');

// App.js Variables
var app = express();
var viewsPath = path.join(__dirname, '/app/views');
app.use(express.static(__dirname + '/app/public'));

// set the port - 3000
app.set('port', process.env.PORT || 3000);

// Form handling
app.use(require('body-parser').urlencoded({
extended:true }));
app.get('/the_test');
// Writes the domain entered in the form to app/data/domain.txt
app.post('/process', function(request, response){
var domain = request.body.domain;

fs.writeFile('app/data/domain.txt', domain, function (err) {
if (err) return console.log(err);
console.log('Your domain has been saved!');;
});

response.redirect(303, '/results');
});

// Routes require
var routes = require('./routes');
app.use('/', routes);
app.use('/results', routes);

app.listen(app.get('port'), function(){
console.log('Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.');
});

我的抓取文件是:

var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');

var scraper = express();
// Scrape the url that was posted
scraper.get('/scrape', function(req, res){
// Scrape this
var url = fs.readFileSync('./app/data/domain.txt', 'utf8');

request(url, function(error, response, html){
if(!error){
var $ = cheerio.load(html);
var header;
var json = { header : ""};

$('.hero-message').filter(function(){
var data = $(this);
header = data.children().first().text();

json.header = header;

});
} else {
console.log(error);
}

fs.writeFile('./app/data/results.json', JSON.stringify(json, null, 4), function(err){
console.log('File successfully written! - Check your project directory for the output.json file');
});

res.send('Check your console!')
});
});

scraper.listen(4000);
console.log('Magic happens on port 4000');
exports = module.exports = scraper;

当我转到 localhost:3000 时,用户可以输入 URL 并点击提交,他们会被重定向到 localhost:3000/results,并且 URL 会记录在 data/domain.txt 中。

当我访问 localhost:4000/scrape 时,抓取工具会激活,从domain.txt 中获取域并抓取它。

我的问题是如何制作这个流畅的程序和/或如何自动激活抓取工具而不是每次都转到 localhost:4000/scrape?我对 Node.js 和 Express 非常陌生,并意识到这是很多难看的代码。

任何提示将不胜感激。

最佳答案

没有必要为您想要做的事情保留两个单独的进程。你能做的就是移动scraper Action

scraper.get("/scrape", function (req, res) {
// code
});

对于主 app.js 文件并从端口 3000 提供所有内容,请确保包含来自 scraper 的所有依赖项。此时,您可能想学习如何使用node's module system保持代码分离和组织。

根据抓取过程所需的时间,您可以执行以下操作之一:

  • 更改 process 操作以执行 scrape 操作当前执行的工作,因此请将域写入文件,然后转到不同的网址从该文件读取并启动该过程,您将捕获该域名并立即提供给抓取工具。
  • 如果抓取工具需要很长时间,并且您希望自动启动抓取作业,您不希望它阻止您的应用程序或在请求期间引发超时。您应该考虑实现工作队列机制。有很多方法可以做到这一点,正确的解决方案在很大程度上取决于应用程序的预期用例。

关于node.js - 如何组合两个都需要监听端口的 Express 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33114227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com