从承诺中获得 JSON
get JSON from a Promise
我正在使用 mocha 来测试在单独的 javascript 文件中编写的承诺。我正在尝试使用 POST 请求将数据发送到 promise,尽管我不确定 url 应该是什么。这是我到目前为止所拥有的,使用请求承诺:
var rp = require('request-promise');
var options = {
method: 'POST',
url: '/algorithm.js',
body: data,
json: true // Automatically stringifies the body to JSON
};
rp(options)
.then(function(body){
count++;
done();
});
错误指出我有一个无效的 url,尽管我不确定 POST 如何在 javascript 文件中承诺。
I am trying to send data to the promise with a POST request
你不能这样做,至少不能直接这样做。
- POST 请求用于向 HTTP 服务器发送数据。
- Promises 是一个 JavaScript 对象,用于处理异步操作。
这些是不同的东西。
algorithm.js
需要包含可以直接调用的代码,在这种情况下,您应该需要该代码,然后调用该函数。
var algorithm = require("algorithm");
if (algorithm.something()) {
count++;
}
done();
… 或者它应该是服务器端 JavaScript 你需要 运行 一个 HTTP 服务器。一旦你 运行 HTTP 服务器,你就可以使用你在问题中写的代码,但你需要提供一个 absolute URL 因为你需要说你正在使用 HTTP 和 localhost 等等。
var options = {
method: 'POST',
url: 'http://localhost:7878/route/to/algorithm',
body: data,
json: true // Automatically stringifies the body to JSON
};
我正在使用 mocha 来测试在单独的 javascript 文件中编写的承诺。我正在尝试使用 POST 请求将数据发送到 promise,尽管我不确定 url 应该是什么。这是我到目前为止所拥有的,使用请求承诺:
var rp = require('request-promise');
var options = {
method: 'POST',
url: '/algorithm.js',
body: data,
json: true // Automatically stringifies the body to JSON
};
rp(options)
.then(function(body){
count++;
done();
});
错误指出我有一个无效的 url,尽管我不确定 POST 如何在 javascript 文件中承诺。
I am trying to send data to the promise with a POST request
你不能这样做,至少不能直接这样做。
- POST 请求用于向 HTTP 服务器发送数据。
- Promises 是一个 JavaScript 对象,用于处理异步操作。
这些是不同的东西。
algorithm.js
需要包含可以直接调用的代码,在这种情况下,您应该需要该代码,然后调用该函数。
var algorithm = require("algorithm");
if (algorithm.something()) {
count++;
}
done();
… 或者它应该是服务器端 JavaScript 你需要 运行 一个 HTTP 服务器。一旦你 运行 HTTP 服务器,你就可以使用你在问题中写的代码,但你需要提供一个 absolute URL 因为你需要说你正在使用 HTTP 和 localhost 等等。
var options = {
method: 'POST',
url: 'http://localhost:7878/route/to/algorithm',
body: data,
json: true // Automatically stringifies the body to JSON
};