Create View in CouchDB

Create View

  • Open the Fauxton URL: http://127.0.0.1:5984/_utils/
  • Choose the specific database.
  • Put your cursor on all documents tab.
  • Click on NEW VIEW.
  • Fill the entries to be added to the view and Save it.
  • A new View is thus created.

Create a app.js file with the below code:
App.js:

const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const NodeCouchdb = require('node-couchdb');
const app = express();
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use (bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.get('/', function(req,res){
res.send('Working........');
});
app.listen(3000, function(){
console.log('Started the server on Port 3000');
});

Execute the following code:

node app

Open localhost:3000 in browser. It will show working…

List Databases:

const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const NodeCouchdb = require('node-couchdb');

const couch = NodeCouchdb({
auth:{
user: 'admin'
password: '98765'
}
});
couch.listDatabases().then(function(dbs){
console.log(dbs);
});

const app = express();
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use (bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.get('/', function(req,res){
res.send('Working........');
});
app.listen(3000, function(){
console.log('Started the server on Port 3000');
});
  • Create a folder named “view”.
  • Create a file “index.ejs” within the folder.
  • Add the below code to the file:
    Hello! I am creating a View. 
  • Now make changes in the “app.js” file:
    res.render('index');

Get View Result:

  • Click on ALL.
  • Click on API URL to copy the URL.
  • Go to app.js and change the code.

App.js:

const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const NodeCouchdb = require('node-couchdb');

const couch = new NodeCouchdb({
auth:{
user:"admin",
password:"98765"
}
});

const dbName ='company';
const viewUrl = '_design/all_companies/_view/all';

couch.listDatabases().then(function(dbs){
console.log(dbs);
});
const app = express();
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use (bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.get('/', function(req,res){
couch.get(dbName, viewUrl).then(
function(data, headers, status){
console.log(data);
res.render('index',{
customer:data
});
},
function(err){
res.send(err);
});
});
app.listen(3000, function(){
console.log('Started the server on Port 3000');
});

Start the server and see the result.

To see the details of the document:
Go to app.js and change the code.

App.js:

const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const NodeCouchdb = require('node-couchdb');

const couch = new NodeCouchdb({
auth:{
user:"admin",
password:"98765"
}
});

const dbName ='company';
const viewUrl = '_design/all_companies/_view/all';
couch.listDatabases().then(function(dbs){
console.log(dbs);
});

const app = express();
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use (bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.get('/', function(req,res){
couch.get(dbName, viewUrl).then(
function(data, headers, status){
console.log(data.data.rows);
res.render('index',{
customer:data
});
},
function(err){
res.send(err);
});
});
app.listen(3000, function(){
console.log('Started the server on Port 3000');
});