转到C文件夹。在已经创建的文件夹“ projects”中创建一个名为“ couchemployees”的文件夹。
打开命令提示符, 然后转到该位置。

文章图片
Start npm init

文章图片
创建一个具有以下代码的文件“ 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');
});

文章图片

文章图片
现在使用以下命令:
npm install express主体解析器ejs node-couchdb – save

文章图片

文章图片
执行以下代码以启动本地服务器:
node app

文章图片
现在服务器已启动:
【Node.js连接CouchDB实例】打开本地浏览器:localhost:3000

文章图片
列出数据库 使用以下代码编辑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');
});

文章图片
创建一个文件夹“ 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');
})

文章图片

文章图片
推荐阅读
- php CouchDB连接实例
- Java CouchDB连接详细实现步骤
- CouchDB的功能介绍
- CouchDB更新文档详细步骤
- CouchDB入门介绍
- CouchDB安装详细步骤
- CouchDB HTTP API详解
- CouchDB Fauxton管理界面
- CouchDB删除文档详细步骤