Node.js CouchDB连接

转到C文件夹。在已经创建的文件夹" projects"中创建一个名为" couchemployees"的文件夹。

打开命令提示符,然后转到该位置。

CouchDB Connect nodejs 1

Start npm init

CouchDB Connect nodejs 2

将文件创建为" app.js",现在的入口点将是app.json:

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('Server is started om Port 3000');
});

CouchDB Connect nodejs 3 CouchDB Connect nodejs 4

现在使用以下命令:

npm install express body-parser ejs node-couchdb --save

CouchDB Connect nodejs 5 CouchDB Connect nodejs 6

执行以下代码以启动本地服务器:

node app

CouchDB Connect nodejs 7

现在服务器已启动,打开本地浏览器: localhost: 3000。

CouchDB Connect nodejs 8

列出数据库

使用以下代码编辑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: 'ajeet'
password: '12345'
}
});
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('Server is started on Port 3000');
});

CouchDB Connect nodejs 9

创建一个文件夹" view",然后在其中创建文件" index.ejs",具有以下代码:

Hello World!

现在对" 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: 'ajeet'
password: '12345'
}
});
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 is started on Port 3000');
})

CouchDB Connect nodejs 10 CouchDB Connect nodejs 11