Node.js CouchDB Tutorial

Node.js CouchDB

To connect Node.JS with CouchDB follow the below steps:

  • Open the C drive.
  • Create a folder named “Students” within an already created folder “Project”.
  • Open the command prompt.
  • Go to the location.
  • Start npm init.
  • Create a file named “app.js”.
  • Add the below code to the file.
  • Here, the entry point is app.json.

    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('In Progress...');  
    });  
    app.listen(3000, function(){  
     console.log('Server started on 3000 Port');  
    });
  • Now execute the below command:
    npm install express body-parser ejs node-couchdb --save
    
  • Start the local server by executing the below code:
    node app
    
  • The server is now started so go to the local browser: localhost:3000

List Databases:

  • Change the 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 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('In Progress...');  
    });  
    app.listen(3000, function(){  
     console.log('Server started on 3000 Port');  
    });
  • Create a file “index.ejs” within an already created folder named “view”.
  • Add the below code to the file. Index.ejs:

    Hello! Connecting Node.JS with CouchDB.
  • Make changes to the “app.js” file again. App.js:

    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.render('index');  
    });  
    app.listen(3000, function(){  
     console.log('Server started on 3000 Port');  
    })
    Please follow and like us:
    Content Protection by DMCA.com