如何使用 node、express 和 mongoose 制作简单的 RESTful 应用程序
How to make simple RESTful app using node, express, and mongoose
我对服务器端开发完全陌生。几乎整个月我都在学习 mongodb, nodeJs, Express。所以我第一次尝试使用这些来制作简单的 RESTful api。但我一遍又一遍地遇到同样的错误。这是代码:
const http = require("http")
const express = require("express")
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const schema = mongoose.Schema;
const url = "mongodb:localhost:27017/app";
//connection to db
const con = mongoose.connect(url,{useNewUrlParser:true,useUnifiedTopology:true})
con.then((db)=>{
console.log("connection is successful");
}).catch((err)=>{
console.log(err);
});
//schema
const plan = new schema({
name:{
type: String,
required:true,
unique:true
},
gender:{
type: String,
required: true
},
date:{
type: Date,
default: Date.now
}
})
const details = mongoose.model("model", plan, "details")
const port = 3000;
const hostName = "localhost";
const app = express();
app.use(bodyParser.json());
app.get("/", (req, res)=>{
res.statusCode = 200;
res.setHeader("Content-Type","application/json")
res.end("This is working well")
});
app.post("/details", (req,res)=>{
details.create(req.body)
.then((details)=>{
console.log("new details is added to database")
res.writeHead(200,{"Content-Type":"application/json"})
res.json(details)
})
.catch((err)=>{
console.log(err)
})
})
app.get("/details", (req,res)=>{
details.find({})
.then((details)=>{
res.writeHead(200,{"Content-Type":"application/json"})
console.log("Recorded documents are: \n")
res.json(details)
})
.catch((err)=>{
console.log(err)
})
})
const server = http.createServer(app)
server.listen(port, hostName,()=>{
console.log(`connected to localhost: ${hostName}:${port}`)
我得到的错误是:
MongoParseError: Invalid connection string
at parseConnectionString (F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongodb\lib\core\uri_parser.js:565:21)
at connect (F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongodb\lib\operations\connect.js:282:3)
at F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongodb\lib\mongo_client.js:223:5
at maybePromise (F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongodb\lib\utils.js:662:3)
at MongoClient.connect (F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongodb\lib\mongo_client.js:219:10)
at F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongoose\lib\connection.js:788:12
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongoose\lib\connection.js:785:19)
at F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongoose\lib\index.js:341:10
at F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:5
at new Promise (<anonymous>)
at promiseOrCallback (F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongoose\lib\helpers\promiseOrCallback.js:30:10)
at Mongoose.connect (F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongoose\lib\index.js:340:10)
at Object.<anonymous> (F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\index.js:7:22)
at Module._compile (internal/modules/cjs/loader.js:1176:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
connected to localhost: localhost:3000
所以我的问题是,我是不是犯了什么错误?如果是,我该如何解决?
URL应该是:
mongodb://localhost:27017/app
这就是它给您“连接错误”的原因。
我对服务器端开发完全陌生。几乎整个月我都在学习 mongodb, nodeJs, Express。所以我第一次尝试使用这些来制作简单的 RESTful api。但我一遍又一遍地遇到同样的错误。这是代码:
const http = require("http")
const express = require("express")
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const schema = mongoose.Schema;
const url = "mongodb:localhost:27017/app";
//connection to db
const con = mongoose.connect(url,{useNewUrlParser:true,useUnifiedTopology:true})
con.then((db)=>{
console.log("connection is successful");
}).catch((err)=>{
console.log(err);
});
//schema
const plan = new schema({
name:{
type: String,
required:true,
unique:true
},
gender:{
type: String,
required: true
},
date:{
type: Date,
default: Date.now
}
})
const details = mongoose.model("model", plan, "details")
const port = 3000;
const hostName = "localhost";
const app = express();
app.use(bodyParser.json());
app.get("/", (req, res)=>{
res.statusCode = 200;
res.setHeader("Content-Type","application/json")
res.end("This is working well")
});
app.post("/details", (req,res)=>{
details.create(req.body)
.then((details)=>{
console.log("new details is added to database")
res.writeHead(200,{"Content-Type":"application/json"})
res.json(details)
})
.catch((err)=>{
console.log(err)
})
})
app.get("/details", (req,res)=>{
details.find({})
.then((details)=>{
res.writeHead(200,{"Content-Type":"application/json"})
console.log("Recorded documents are: \n")
res.json(details)
})
.catch((err)=>{
console.log(err)
})
})
const server = http.createServer(app)
server.listen(port, hostName,()=>{
console.log(`connected to localhost: ${hostName}:${port}`)
我得到的错误是:
MongoParseError: Invalid connection string
at parseConnectionString (F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongodb\lib\core\uri_parser.js:565:21)
at connect (F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongodb\lib\operations\connect.js:282:3)
at F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongodb\lib\mongo_client.js:223:5
at maybePromise (F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongodb\lib\utils.js:662:3)
at MongoClient.connect (F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongodb\lib\mongo_client.js:219:10)
at F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongoose\lib\connection.js:788:12
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongoose\lib\connection.js:785:19)
at F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongoose\lib\index.js:341:10
at F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:5
at new Promise (<anonymous>)
at promiseOrCallback (F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongoose\lib\helpers\promiseOrCallback.js:30:10)
at Mongoose.connect (F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\node_modules\mongoose\lib\index.js:340:10)
at Object.<anonymous> (F:\Node.Js files\Coursera\week2\#3 REST API with Express, node and mongoose\#1 Example\index.js:7:22)
at Module._compile (internal/modules/cjs/loader.js:1176:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
connected to localhost: localhost:3000
所以我的问题是,我是不是犯了什么错误?如果是,我该如何解决?
URL应该是:
mongodb://localhost:27017/app
这就是它给您“连接错误”的原因。