Node.js CouchDB连接基础教程文档
收录于 2023-04-20 00:10:05 · بالعربية · English · Español · हिंदीName · 日本語 · Русский язык · 中文繁體
转到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 body-parser ejs node-couchdb--save
执行以下代码以启动本地服务器:
node app
现在服务器已启动:
打开本地浏览器: 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'); })