【发布时间】:2019-09-28 08:26:54
【问题描述】:
流动的代码成功地将 mongoose 与 localhost 和 Heroku 上的 mlab 数据库连接起来。但它不适用于 Namecheap 节点 js 服务器。
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: "your@email.com" });
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}`);
});
当我点击 root('/') 路由时,它在 Namecheap 服务器上运行良好,但是当我点击 test('test') 路由时,它会在很长一段时间后响应“从应用程序收到不完整的响应”。那么mongoose和Namecheap共享主机上的数据库有什么连接方式呢?
【问题讨论】:
-
永远不要分享您的密码
-
你能打印userData吗
-
@mehta-rohan 是的,在本地主机中是可能的
-
Namecheap 中一定有一些出站请求配置需要检查。
标签: node.js mongodb mongoose cpanel shared-hosting