如題,
我有三個檔案來創建nodejs的server,
index.js
負責開啟server
app.get('/', (req, res) => {
res.send('Hello World!')
}) //localhost:port/ 可以顯示
//routes
const api = require('./routes/crud');
app.use( '/api',api ); //但採用 localhost:port/api/create 會顯示 cannot post
model/Schema.js
負責採用mongoose創建儲存的資料格式
routes/crud.js
負責使用route做CRUD操作
const express = require('express');
const apiRouter = express.Router();
const schema = require('../model/Schema');
apiRouter.post('/create', (req, res) => {
const body = req.body
if (!body) {
return res.status(400).json({
success: false,
error: 'You must provide a db_schema',
})
}
const db_schema = new schema(body)
if (!db_schema) {
return res.status(400).json({ success: false, error: err })
}
db_schema
.save()
.then(() => {
return res.status(201).json({
success: true,
id: movie._id,
message: 'db_schema created!',
})
})
.catch(error => {
return res.status(400).json({
error,
message: 'db_schema not created!',
})
})
})
module.exports = apiRouter;
懇請各位大大解答,感謝
index.js
const app = require('express')();
const routers = require('./routers');
const port = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.status(200).send({ message: 'Hello world' }).end;
});
app.use('/api', routers);
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
routers.js
const router = require('express').Router();
router.get('/dragonH', (req, res) => {
res.status(200).send({ message: 'I am dragonH' }).end();
});
module.exports = router;
http://127.0.0.1:3000/
{
message: "Hello world"
}
http://127.0.0.1:3000/api/dragonH
{
message: "I am dragonH"
}
感謝 D大,你的 get 方式可以run,
但是如果採用我的
routers.js
Router.post('/create', (req, res) => {
const body = req.body
if (!body) {
return res.status(400).json({
success: false,
error: 'You must provide a schema',
})
}
const schema = new Schema(body)
if (!schema) {
return res.status(400).json({ success: false, error: err })
}
schema
.save()
.then(() => {
return res.status(201).json({
success: true,
id: schema._id,
message: 'schema created!',
})
})
.catch(error => {
return res.status(400).json({
error,
message: 'Schema not created!',
})
})
})
http://localhost:port/api/create
一樣會顯示 Cannot GET /api/create 的問題
目前有採用POSTMAN
POST 到 http://localhost:port/api/create 的網址
輸入格式
{
"欄位1" :"欄位1"
"欄位2" :"欄位2"
.
.
.
}
但是它回傳
"error": {
"errors": {
"欄位1": {
"message": "Path `欄位1` is required.",
"name": "ValidatorError",
"properties": {
"message": "Path `欄位1` is required.",
"type": "required",
"path": "欄位1"
},
"kind": "required",
"path": "欄位1"
},
"欄位2": {
"message": "Path `欄位2` is required.",
"name": "ValidatorError",
"properties": {
"message": "Path `欄位2` is required.",
"type": "required",
"path": "欄位2"
},
"kind": "required",
"path": "欄位2"
},
.
.
.
一樣會顯示 Cannot GET /api/create 的問題
因為你是寫 Router.post
但是它回傳
我不會通靈
不知道你 schema 裡面啥來驗證
總之應該就是 schema 不符
蛤 你要用 form-urlencoded 喔
直接 raw 傳 json 比較快
不過你的 express 那需要加個 body-parser 的 middleware
index.js
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
原本是用raw 但就會回傳
"error": {
"errors": {
"欄位1": {
"message": "Path `欄位1` is required.",
"name": "ValidatorError",
"properties": {
"message": "Path `欄位1` is required.",
"type": "required",
"path": "欄位1"
},
"kind": "required",
"path": "欄位1"
},
剛剛去查找了不太懂的地方,並紀錄一下連結給有需要的人參考
postman中 form-data、x-www-form-urlencoded、raw、binary的区别
那可能就要看你 body
長怎樣了
Schema.js
const schema = new Schema({
欄位1 :{
type : String,
required : true
},
欄位2 :{
type : String,
required : true
},
.
.
.
});
module.exports = mongoose.model('db_collection_name',schema); //這邊提醒一下mongoose存入的colletion名稱會自動變全小寫且後面加一個 s
存入postman的 raw body 資料格式
{
"欄位1" :"IamString1"
"欄位2" :"IamString2"
.
.
.
}