我如何使用纯 NodeJS 从 CouchDB Cloudant 获取和显示数据?
How can I get and show data from CouchDB Cloudant with pure NodeJS?
我认为这是许多初学者的基本问题。我也有同样的疑问。
如何轻松地从 PURE NodeJS 代码发送请求并变得简单 JSON?我怎样才能得到发送数据到网络的响应?
首先向数据库发送请求并向控制台显示:
https://nodejs.org/api/https.html
var https = require('https');
var db_options = {
"auth": "{{name}}:{{pwd}}",
"host": "{{name}}.cloudant.com",
"port": "443", // or 80
"path": "/{{db_name}}/_all_docs"
}
https.request(db_options, function(response) { // or http
var out = '';
response.on('data', function(data) {
out += data;
});
response.on('end', function() {
console.log(out);
})
})
.on('error', function(err) {
console.log(err);
})
.end();
然后根据基本示例将其简单地包装到 web 服务器 Node.js:
https://nodejs.org/api/synopsis.html
var http = require('http');
var server_ipAddr = '127.0.0.1';
var server_port = 8080;
var server_http = http.createServer(function(req, res) {
...
response.on('end', function() {
res.end(out); // <-- add or replace "console.log(out);"
})
...
}).listen(server_port, server_ipAddr);
您可以单独使用这些代码。一起看起来像这样:
var http = require('http');
var server_ipAddr = '127.0.0.1';
var server_port = 8080;
var server_http = http.createServer(function(req, res) {
var https = require('https');
var db_options = {
"auth": "{{name}}:{{pwd}}",
"host": "{{name}}.cloudant.com",
"port": "443", // or 80
"path": "/{{db_name}}/_all_docs"
}
https.request(db_options, function(response) { // or http
var out = '';
response.on('data', function(data) {
out += data;
});
response.on('end', function() {
console.log(out);
res.end(out);
})
})
.on('error', function(err) {
console.log(err);
})
.end();
}).listen(server_port, server_ipAddr);
如果你 运行 它你会得到这样的东西:
$ node cloudant.get.alldoc.js
{"total_rows":148,"offset":0,"rows":[
{"id":"0fdda996e7b4f11b8a5ab6c9aa002151","key":"0fdda996e7b4f11b8a5ab6c9aa002151","value":{"rev":"1-6eb8654a7ac5a7cb810e87ef30b2a9d5"}},
...
]}
我希望这对某人有所帮助。
我认为这是许多初学者的基本问题。我也有同样的疑问。
如何轻松地从 PURE NodeJS 代码发送请求并变得简单 JSON?我怎样才能得到发送数据到网络的响应?
首先向数据库发送请求并向控制台显示:
https://nodejs.org/api/https.html
var https = require('https');
var db_options = {
"auth": "{{name}}:{{pwd}}",
"host": "{{name}}.cloudant.com",
"port": "443", // or 80
"path": "/{{db_name}}/_all_docs"
}
https.request(db_options, function(response) { // or http
var out = '';
response.on('data', function(data) {
out += data;
});
response.on('end', function() {
console.log(out);
})
})
.on('error', function(err) {
console.log(err);
})
.end();
然后根据基本示例将其简单地包装到 web 服务器 Node.js:
https://nodejs.org/api/synopsis.html
var http = require('http');
var server_ipAddr = '127.0.0.1';
var server_port = 8080;
var server_http = http.createServer(function(req, res) {
...
response.on('end', function() {
res.end(out); // <-- add or replace "console.log(out);"
})
...
}).listen(server_port, server_ipAddr);
您可以单独使用这些代码。一起看起来像这样:
var http = require('http');
var server_ipAddr = '127.0.0.1';
var server_port = 8080;
var server_http = http.createServer(function(req, res) {
var https = require('https');
var db_options = {
"auth": "{{name}}:{{pwd}}",
"host": "{{name}}.cloudant.com",
"port": "443", // or 80
"path": "/{{db_name}}/_all_docs"
}
https.request(db_options, function(response) { // or http
var out = '';
response.on('data', function(data) {
out += data;
});
response.on('end', function() {
console.log(out);
res.end(out);
})
})
.on('error', function(err) {
console.log(err);
})
.end();
}).listen(server_port, server_ipAddr);
如果你 运行 它你会得到这样的东西:
$ node cloudant.get.alldoc.js
{"total_rows":148,"offset":0,"rows":[
{"id":"0fdda996e7b4f11b8a5ab6c9aa002151","key":"0fdda996e7b4f11b8a5ab6c9aa002151","value":{"rev":"1-6eb8654a7ac5a7cb810e87ef30b2a9d5"}},
...
]}
我希望这对某人有所帮助。