Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript NodeJS:创建散列并返回函数值_Javascript_Jquery_Node.js_Variables - Fatal编程技术网

Javascript NodeJS:创建散列并返回函数值

Javascript NodeJS:创建散列并返回函数值,javascript,jquery,node.js,variables,Javascript,Jquery,Node.js,Variables,我有一个需要提取的标签列表。该列表称为list 我试图找到所有与列表相对应的“og:*”元,这些元以获取的html格式提供。然后我需要用JSON向用户返回一个包含这些元标记的散列。但是过程方法返回未定义而不是散列 var http = require('http'); var url = require('url'); var request = require('request'); var jsdom = require("jsdom"); var fs = require('fs'); v

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

我试图找到所有与列表相对应的“og:*”元,这些元以获取的html格式提供。然后我需要用JSON向用户返回一个包含这些元标记的散列。但是
过程
方法返回
未定义
而不是散列

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');

因为
请求
是异步的,所以还需要使
进程
异步。这意味着让
process
接受一个回调参数,一旦
meta
可用,它就会调用该参数。现在,
进程
请求
回调填充它之前返回
meta

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);

尽量不要使用
process
作为该函数的名称,它是一个NodeJS变量,您可以通过它访问与节点进程本身相关的信息@JohnyHK,如果可以的话,这是一个附属问题:
request()
是否返回承诺,如客户端jQuery中的
$.ajax()
?@Beetroot据我所知,不是。请参阅此处的文档: