我正在跟踪一个表达如何连接到mongodb的快速练习。当我运行app.js文件时,收到以下消息:
它应该显示我的console.log消息"' MongoDB Conneced ....'"表明它已连接但不是。
const express = require('express');
const exphbs = require('express-handlebars')
const mongoose = require('mongoose');
const app = express();
//Map global promise - get rid of warning
mongoose.Promise = global.Promise;
//connect to mongoose
mongoose.connect('mongodb://localhost/vidjot-dev')
.then(()=>console.log('MongoDB Conneced....'))
.catch(err => console.log(err));
//handlebars Middleware
app.engine('handlebars', exphbs({
defaultLayout: 'main'
}));
app.set('view engine', 'handlebars');
//Index Route
app.get('/', (req, res)=>{
const myTitle="Welcome.";
res.render('INDEX', {
title:myTitle
});
});
//About Route
app.get('/about', (req, res)=>{
res.render('ABOUT');
})
const port = 5500;
app.listen(port, ()=> {
console.log(`Server started on port ${port}`);
/*Same as above which is ES6 */
//console.log('Server started on port '+port);
})
答案 0 :(得分:0)
确保已在系统中安装mongodb,然后设置其路径。
最后,只需在终端mongod
中运行命令
注意:还请为数据库创建一个文件夹,例如它要求您创建的文件夹:c:/ db。
然后尝试再次连接