【问题标题】:Need some help while calling webservice using node.js?在使用 node.js 调用 web 服务时需要一些帮助吗?
【发布时间】:2013-12-13 05:31:34
【问题描述】:

我正在从这个例子中得到帮助。
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 命令提示符

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>

【问题讨论】:

  • 真正的问题是什么?
  • 我想使用 NODe.js 调用 web 服务
  • 这就是您想要的,但是您遇到了什么问题?您还没有添加任何关于如何/是否实际启动节点(a'la“node test.js”)以及您的结果和预期结果是什么(如果不正确)的信息。
  • 我收到错误我的 package.json 有错误
  • 你能给出一些如何开始的步骤吗?我对这个问题感到沮丧

标签: javascript json node.js


【解决方案1】:

您刚刚初始化了 node.js 环境,现在您需要实际运行程序。

node json.js // whetever the file name that you saved the script with

【讨论】:

    【解决方案2】:

    Node.js 是 javascript 脚本的一个新概念。我花了一段时间试图理解它,尽管经过这么多小时的谷歌搜索和学习教程,直到我遇到了这个网站。很简单的学习,终于搞定了。

    http://net.tutsplus.com/tutorials/javascript-ajax/introduction-to-express

    然后您将看到上面的脚本尚未完成。希望对您有所帮助(我花了 30 分钟的时间 - 只需按照教程复制并粘贴每个过程即可)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      • 1970-01-01
      相关资源
      最近更新 更多