The flowing code successfully connects mongoose with the mlab database on localhost and Heroku. But it’s not working on Namecheap node js Server.
const express = require("express");
const mongoose = require("mongoose");
const port = parseInt(process.env.PORT, 10) || 3000;
const server = express();
mongoose.connect("mongodb://@ds213645.mlab.com:13645/techblog", {
useNewUrlParser: true,
auth: {
user: "user",
password: "pass"
}
});
const Schema = mongoose.Schema;
const userSchema = new Schema(
{
name: String,
email: String
},
{ timestamps: true }
);
const User = mongoose.model("User", userSchema);
server.get("/test", async (req, res) => {
const user = new User({ name: "SomeName", email: "[email protected]" });
const userData = await user.save();
res.send(userData);
});
server.get("/", async (req, res) => {
res.send("working");
});
server.listen(port, err => {
if (err) throw err;
console.log(`> Ready on ${port}`);
});
When I hit the root(‘/’) route it works perfectly on Namecheap server but when I hit test(‘test’) route it response ‘Incomplete response received from application’ after a long time. So what is the way to connect mongoose with the database on Namecheap shared hosting?
3
Answers
Issue solved. The problem was Namecheap. Namecheap blocked outgoing request on port 13645. After contacting them they opened outgoing request on port 13645
at the first is used in local or MLAB if is MLAB try the same
mongoose.connect(MONGO_URI);
mongoose.connection
if used the MongoDB in local try to run the mongo server from go to the location
C:Program FilesMongoDBServer4.0bin and run mongo then run Mongod after then try to run the project enter image description here
“//”name user and password
: MustName_user:password //@ds213645.mlab.com:13645/techblog”, {
Contact namecheap support and ask them to open the ports below
ports:
27017
3000
443 and
80