问题描述:

can you please tell me how to call json webservice and get output ?I need some steps how to do that task.

I am just started using node.js.

I create a js file using command promt i run that file

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

[email protected] node_modules\request

├── [email protected]

├── [email protected]

├── [email protected]

├── [email protected]

├── [email protected]

├── [email protected]

├── [email protected]

├── [email protected]

├── [email protected] ([email protected])

├── [email protected] ([email protected], [email protected], [email protected], [email protected])

├── [email protected] ([email protected], [email protected], [email protected])

└── [email protected] ([email protected], [email protected])

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>

网友答案:

I guess you did not install the request module, but you are trying to use it. Navigate to your project folder in your console and install the request module with:

npm install request

Then try again, the Cannot find module error should be gone.

相关阅读:
Top