我正在从这个例子中获得帮助
Consume JSON web service from Node/Express request?
我在目录C:/ programfile / nodejs /
中创建了一个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);
}
});
from命令promt我运行这个脚本
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 "Program Files"
C:\Program Files>cd nodejs
C:\Program Files\nodejs>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sane defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
name: (nodejs)
version: (0.0.0)
description:
git repository:
keywords:
author:
license: (BSD-2-Clause)
About to write to C:\Program Files\nodejs\package.json:
{
"name": "nodejs",
"version": "0.0.0",
"main": "a.js",
"dependencies": {
"npm": "~1.3.17",
"jsonraver": "~0.5.0",
"request": "~2.29.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "BSD-2-Clause"
}
Is this ok? (yes)
C:\Program Files\nodejs>npm install request --save
npm WARN package.json nodejs@0.0.0 No description
npm WARN package.json nodejs@0.0.0 No repository field.
npm WARN package.json nodejs@0.0.0 No README data
npm http GET https://registry.npmjs.org/request
npm http 304 https://registry.npmjs.org/request
npm http GET https://registry.npmjs.org/qs
npm http GET https://registry.npmjs.org/json-stringify-safe
npm http GET https://registry.npmjs.org/node-uuid
npm http GET https://registry.npmjs.org/forever-agent
npm http GET https://registry.npmjs.org/tough-cookie
npm http GET https://registry.npmjs.org/mime
npm http GET https://registry.npmjs.org/tunnel-agent
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/aws-sign2
npm http GET https://registry.npmjs.org/hawk
npm http 304 https://registry.npmjs.org/node-uuid
npm http 304 https://registry.npmjs.org/forever-agent
npm http 304 https://registry.npmjs.org/json-stringify-safe
npm http 304 https://registry.npmjs.org/tough-cookie
npm http 304 https://registry.npmjs.org/qs
npm http 304 https://registry.npmjs.org/mime
npm http 304 https://registry.npmjs.org/tunnel-agent
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/aws-sign2
npm http 304 https://registry.npmjs.org/hawk
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/cryptiles
npm http GET https://registry.npmjs.org/boom
npm http GET https://registry.npmjs.org/sntp
npm http GET https://registry.npmjs.org/hoek
npm http 304 https://registry.npmjs.org/async
npm http 304 https://registry.npmjs.org/assert-plus/0.1.2
npm http 304 https://registry.npmjs.org/combined-stream
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/punycode
npm http 304 https://registry.npmjs.org/cryptiles
npm http 304 https://registry.npmjs.org/boom
npm http 304 https://registry.npmjs.org/sntp
npm http 304 https://registry.npmjs.org/hoek
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
├── json-stringify-safe@5.0.0
├── forever-agent@0.5.0
├── tunnel-agent@0.3.0
├── qs@0.6.6
├── oauth-sign@0.3.0
├── aws-sign2@0.5.0
├── 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, boom@0.4.2, sntp@0.2.4, 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:\Program Files\nodejs>
答案 0 :(得分:0)
您刚刚初始化了node.js环境,现在您需要实际运行该程序。
node json.js // whetever the file name that you saved the script with
答案 1 :(得分:0)
在javascript中编写脚本时,Node.js是一个新概念。我花了一段时间试图理解它,即使经过这么多时间谷歌搜索和学习教程,直到我遇到这个网站。非常简单的学习,终于让我的头上有轻盈的俱乐部。
http://net.tutsplus.com/tutorials/javascript-ajax/introduction-to-express
然后,您将看到上面的脚本尚未完成。希望有所帮助(花了30分钟的时间 - 只需完成并复制n'通过教程粘贴每个过程。)