【发布时间】:2021-02-15 05:15:33
【问题描述】:
当我尝试在本地主机中发布表单时,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.
下面是我的 package.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 但是当我在终端中打开 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 :: 时出错,由 :: 目标机器主动拒绝,无法建立连接。 : 连接@src/mongo/shell/mongo.js:374:17 @(连接):2:6 异常:连接失败 退出代码 1
【问题讨论】:
-
ECONNREFUSED 127.0.0.1:27017您的 MongoDB 服务器没有运行。启动您的 MongoDB 服务。 -
正如我已经提到的,我首先在一个终端打开 mongod,在第二个终端打开 mongo,然后在不同的终端运行 nodejs。之后我正在尝试提交帖子。当我尝试提交帖子时会发生错误。
-
你能说一下你用的是什么机器吗?视窗?苹果? Linux?
标签: javascript node.js json mongodb mongoose