Node(16) working with couchDB

Apache CouchDB™ is a database that uses JSON for documents, JavaScript for MapReduce queries, and regular HTTP for an API. It is a NoSQL database. It is not a relational database.


Node example to get database names using GET method:

var http = require('http');

http.createServer( function( req, res ){
	var options = {
		host: 'localhost',
		port: '5984',
		method: 'GET',
		path: '/_all_dbs'
	}
	
	//make a http request to database
	var request = http.request( options, function(response){
		var responseBody = "";
		
		//receiving data
		response.on("data", function( chunk ){
			responseBody +=chunk;
		});
		
		//display function
		response.on( "end", function(chunk){
			res.writeHead( 200, { 'Content-Type':'text/plain' });
			res.write( responseBody);
			res.end();
		});
		
		//handling error
		request.on('error', function(e){  
			console.log( 'problem with request: ' + e.message );  
		});  

	});
	request.end();
}).listen(9000);


Create database using PUT method:

var http = require( 'http');
var options = {
	host: 'localhost',
	port: '5984',
	method: 'PUT',
	path: '/dbname'
}

//make a database request
var req = http.request( options, function(response){
	response.on( "end", function(){
	if( response.statusCode == 201){
		console.log("Database successfully created.");
	}else{
		console.log( "Could not createdatabase." );
	}
	});
});

//handling error
req.on('error', function(e){  
    console.log( 'problem with request: ' + e.message );  
});  

There are modules for working with couchdb:

npm install felix-couchdb


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值