0

我有一个需要提取的标签列表。该列表称为list.

我正在尝试查找与列表相对应且在获取的 html 中可用的所有 'og:*' 元数据。然后我需要以包含这些元标记的 JSON 格式向用户返回一个哈希值。但是该process方法返回undefined而不是哈希。

var http = require('http');
var url = require('url');
var request = require('request');
var jsdom = require("jsdom");
var fs = require('fs');
var cssom = require('cssom');

var list = ['title', 'description']; //here the og-tags I need to extract
var meta = {};

function process(url) {
  request(url, function (error, response, body) {
    if (!error && response.statusCode == 200) {  
      jsdom.env({
        html: body,
        scripts: [
          'http://code.jquery.com/jquery-1.5.min.js'
        ],
        done: function(errors, window) {
          var $ = window.$;

          $('meta[property^="og:"]').each(function() {
            for (var element in list) {
              if ($(this).attr('property') == 'og:' + list[element]) {
                meta[list[element]] = $(this).attr('content');

                // this works well, if I do console.log(meta), I get the hash correctly filled.
              }
            }
          });
        }
      });
    }
  });

  return meta; // this is where the probleme is. This return undefined.
}


http.createServer(function (request, response) {
  request.setEncoding('utf8');
  response.writeHead(200, {'Content-Type': 'text/plain'});

  process(url.parse(request.url, true).query['content'], function(result) {
    console.log(result); // prints no result
  });

  response.end();
}).listen(8124);

console.log('Server running at http://0.0.0.0:8124');
4

1 回答 1

1

因为request是异步的,所以也需要process异步。这意味着process接受一个它会调用一次的回调参数meta是可用的。就像现在一样,在回调填充它之前process返回。metarequest

function process(url, callback) {
  request(url, function (error, response, body) {
    if (!error && response.statusCode == 200) {  
      jsdom.env({
        html: body,
        scripts: [
          'http://code.jquery.com/jquery-1.5.min.js'
        ],
        done: function(errors, window) {
          var $ = window.$;

          $('meta[property^="og:"]').each(function() {
            for (var element in list) {
              if ($(this).attr('property') == 'og:' + list[element]) {
                meta[list[element]] = $(this).attr('content');
                callback(null, meta);
              }
            }
          });
        }
      });
    } else {
      callback(error);
    }
  });
}


http.createServer(function (request, response) {
  request.setEncoding('utf8');
  response.writeHead(200, {'Content-Type': 'text/plain'});

  process(url.parse(request.url, true).query['content'], function(error, result) {
    console.log(result); // prints no result
  });

  response.end();
}).listen(8124);
于 2012-08-20T20:26:37.293 回答