当我试图在本地主机中发布表单时,vscode终端出现错误。我正在使用mongodb为我的网站数据库。下面是我的app.js代码-
const express = require("express");
const path = require("path");
const bodyparser = require("body-parser");
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/contactdance', {useNewUrlParser: true, useUnifiedTopology: true});
const app = express();
const port = 8000;
//DEFINE MONGOOSE SCHEMA
const contactSchema = new mongoose.Schema({
name: String,
phone: String,
email: String,
address: String,
desc: String
});
const contact = mongoose.model('Contact', contactSchema);
// EXPRESS SPECIFIC STUFF
app.use('/static', express.static('static')) // For serving static files
app.use(express.urlencoded())
// PUG SPECIFIC STUFF
app.set('view engine', 'pug') // Set the template engine as pug
app.set('views', path.join(__dirname, 'views')) // Set the views directory
// ENDPOINTS
app.get('/', (req, res)=>{
const params = { }
res.status(200).render('home.pug', params);
})
app.get('/contact', (req, res)=>{
const params = { }
res.status(200).render('contact.pug', params);
})
app.post('/contact', (req, res)=>{
var myData = new contact(req.body);
myData.save().then(()=>{
res.send("This item has been saved successfully")
}).catch(()=>{
res.status(400).send("Item has not been saved")
});
// res.status(200).render('contact.pug');
});
// START THE SERVER
app.listen(port, ()=>{
console.log(`The application started successfully on port ${port}`);
});
以下是投递表格时出现的错误-
(node:15248) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at NativeConnection.Connection.openUri (E:\program files\rr\Web Development\dance_website\node_modules\mongoose\lib\connection.js:803:32)
at E:\program files\rr\Web Development\dance_website\node_modules\mongoose\lib\index.js:342:10
at E:\program files\rr\Web Development\dance_website\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:5
at new Promise (<anonymous>)
at promiseOrCallback (E:\program files\rr\Web Development\dance_website\node_modules\mongoose\lib\helpers\promiseOrCallback.js:30:10)
at Mongoose.connect (E:\program files\rr\Web Development\dance_website\node_modules\mongoose\lib\index.js:341:10)
at Object.<anonymous> (E:\program files\rr\Web Development\dance_website\app.js:5:10)
at Module._compile (internal/modules/cjs/loader.js:1015:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
at Module.load (internal/modules/cjs/loader.js:879:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
at internal/main/run_main_module.js:17:47
(node:15248) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:15248) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate
the Node.js process with a non-zero exit code.
下面是我的包。json-
{
"name": "dance_website",
"version": "1.0.0",
"description": "This is a website for a polular dance academy",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Harry bhai",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"mongoose": "^5.10.11",
"pug": "^3.0.0"
},
"devDependencies": {}
}
我在我的VSCode里打开了三个终端-
>
用于nodejs
为了蒙神
但是当我在终端中打开mongo时,我得到了一个错误。以下是错误-
MongoDB shell版本v4.4.1连接到:MongoDB://127.0.0.1:27017/?compressors=disabled&gssapiserviceName=MongoDB错误:无法连接到服务器127.0.0.1:27017,连接尝试失败:SocketException:连接到127.0.0.1:27017时出错::原因::由于目标计算机主动拒绝,无法建立连接。:connect@src/mongo/shell/mongo.js:374:17@(connect):2:6异常:连接失败退出,代码为1
您收到的两个错误意味着同一件事:您的MongoDB服务器没有运行。
请仔细检查mongo本身的安装说明,而不是mongoose或mongo shell的安装说明。