转到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
现在服务器已启动:
打开本地浏览器: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');
})
评论前必须登录!
注册