I got an error in vscode terminal while I am trying to post a form in my localhost. I am using mongodb for database for my website. Below are my app.js code-
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}`);
});
Below are the errors while trying to post the form-
(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.
Below are my 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": {}
}
I opened three terminal in my vscode-
for nodejs
for mongod
for mongo but when I open mongo in the terminal I got an error. Below are the errors-
MongoDB shell version v4.4.1 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: No connection could be made because the target machine actively refused it. : connect@src/mongo/shell/mongo.js:374:17 @(connect):2:6 exception: connect failed exiting with code 1
ECONNREFUSED 127.0.0.1:27017your MongoDB server is not running. Start your MongoDB service. - Jeremy Thille