使用node.js调用Web服务时出错?

时间:2013-12-13 07:11:59

标签: jquery node.js

你能告诉我如何调用json webservice并获得输出吗?我需要一些步骤来完成这项任务。 我刚开始使用node.js. 我使用命令promt创建一个js文件我运行该文件

var request = require("request");
request.get("http://ergast.com/api/f1/current/last/results.json", function (err, res, body) {
    if (!err) {
        var resultsObj = JSON.parse(body);
        //Just an example of how to access properties:
        console.log(resultsObj.MRData);
    }
});


   Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\nksharma.Q3TECH>cd..

C:\Documents and Settings>cd..

C:\>cd node

C:\node>npm install request
npm http GET https://registry.npmjs.org/request
npm http 304 https://registry.npmjs.org/request
npm http GET https://registry.npmjs.org/json-stringify-safe
npm http GET https://registry.npmjs.org/qs
npm http GET https://registry.npmjs.org/forever-agent
npm http GET https://registry.npmjs.org/node-uuid
npm http GET https://registry.npmjs.org/tough-cookie
npm http GET https://registry.npmjs.org/form-data
npm http GET https://registry.npmjs.org/oauth-sign
npm http GET https://registry.npmjs.org/http-signature
npm http GET https://registry.npmjs.org/mime
npm http GET https://registry.npmjs.org/tunnel-agent
npm http GET https://registry.npmjs.org/hawk
npm http GET https://registry.npmjs.org/aws-sign2
npm http 304 https://registry.npmjs.org/forever-agent
npm http 304 https://registry.npmjs.org/node-uuid
npm http 304 https://registry.npmjs.org/tough-cookie
npm http 304 https://registry.npmjs.org/form-data
npm http 304 https://registry.npmjs.org/oauth-sign
npm http 304 https://registry.npmjs.org/http-signature
npm http 304 https://registry.npmjs.org/mime
npm http 304 https://registry.npmjs.org/tunnel-agent
npm http 304 https://registry.npmjs.org/hawk
npm http 304 https://registry.npmjs.org/aws-sign2
npm http 304 https://registry.npmjs.org/json-stringify-safe
npm http 304 https://registry.npmjs.org/qs
npm http GET https://registry.npmjs.org/combined-stream
npm http GET https://registry.npmjs.org/async
npm http GET https://registry.npmjs.org/asn1/0.1.11
npm http GET https://registry.npmjs.org/assert-plus/0.1.2
npm http GET https://registry.npmjs.org/ctype/0.5.2
npm http GET https://registry.npmjs.org/punycode
npm http GET https://registry.npmjs.org/sntp
npm http GET https://registry.npmjs.org/boom
npm http GET https://registry.npmjs.org/cryptiles
npm http GET https://registry.npmjs.org/hoek
npm http 304 https://registry.npmjs.org/async
npm http 304 https://registry.npmjs.org/asn1/0.1.11
npm http 304 https://registry.npmjs.org/ctype/0.5.2
npm http 304 https://registry.npmjs.org/assert-plus/0.1.2
npm http 304 https://registry.npmjs.org/punycode
npm http 304 https://registry.npmjs.org/sntp
npm http 304 https://registry.npmjs.org/boom
npm http 304 https://registry.npmjs.org/cryptiles
npm http 304 https://registry.npmjs.org/hoek
npm http 304 https://registry.npmjs.org/combined-stream
npm http GET https://registry.npmjs.org/delayed-stream/0.0.5
npm http 304 https://registry.npmjs.org/delayed-stream/0.0.5
request@2.29.0 node_modules\request
├── forever-agent@0.5.0
├── aws-sign2@0.5.0
├── json-stringify-safe@5.0.0
├── tunnel-agent@0.3.0
├── oauth-sign@0.3.0
├── qs@0.6.6
├── mime@1.2.11
├── node-uuid@1.4.1
├── tough-cookie@0.9.15 (punycode@1.2.3)
├── hawk@1.0.0 (cryptiles@0.2.2, sntp@0.2.4, boom@0.4.2, hoek@0.9.1)
├── http-signature@0.10.0 (assert-plus@0.1.2, asn1@0.1.11, ctype@0.5.2)
└── form-data@0.1.2 (async@0.2.9, combined-stream@0.0.4)

C:\node>node b.js
{ xmlns: 'http://ergast.com/mrd/1.2',
  series: 'f1',
  url: 'http://ergast.com/api/f1/current/last/results.json',
  limit: '30',
  offset: '0',
  total: '22',
  RaceTable: { season: '2013', round: '19', Races: [ [Object] ] } }

C:\node>

1 个答案:

答案 0 :(得分:0)

我猜你没有安装请求模块,但是你正在尝试使用它。导航到控制台中的项目文件夹,并使用以下命令安装请求模块:

npm install request

然后再试一次,找不到模块错误。