获取错误 <pre>Cannot POST /api/category/create/62545ea650265cda5b08f10d</pre>
Getting error <pre>Cannot POST /api/category/create/62545ea650265cda5b08f10d</pre>
我正在学习 MERN 开发,并尝试创建 API 以使用邮递员添加类别。但是低于错误
Cannot POST /api/category/create/62545ea650265cda5b08f10d
我在此处添加邮递员的代码和屏幕截图,如有任何帮助,我将不胜感激!
controller\category.js
const Category = require("../models/category");
//const { errorHandler } = require("../helpers/dbErrorHandler");
exports.create = (req, res) => {
const category = new Category(req.body);
category.save((err, data) => {
if (err) {
return res.status(400).json({
// error: errorHandler(err)
});
}
res.json({ data });
})
}
model\category.js
const mongoose = require('mongoose');
const categorySchema = new mongoose.Schema({
name: {
type: String,
trim: true,
required: true,
maxlength: 32
}
}, { timestamps: true }
);
module.exports = mongoose.model("Category", categorySchema);
routes/category.js
const express = require('express');
const router = express.Router();
const { create } = require('../controllers/category');
const { requireSignin, isAuth, isAdmin } = require('../controllers/auth');
const { userById } = require('../controllers/user');
router.post("/category/create:userId", requireSignin, isAuth, isAdmin, create);
router.param('userId', userById);
module.exports = router;
app.js
// const express = require('express')
// const app = express()
// require('dotenv').config()
// app.get('/',(req, res)=>{
// res.send('hello from node updated');
// })
// const port = process.env.PORT || 8000
// app.listen(port, ()=>{
// console.log(`Servver is running on port ${port}`)
// });
const express = require('express')
// import mongoose
const mongoose = require('mongoose');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const expressValidator = require('express-validator');
// load env variables
const dotenv = require('dotenv');
dotenv.config();
//import routes
const authRoutes = require('./routes/auth');
const userRoutes = require('./routes/user');
const categoryRoutes = require('./routes/category');
//app
const app = express();
//db connection
mongoose.connect(
process.env.DATABASE,
{
useNewUrlParser: true
}
)
.then(() => console.log('DB Connected'));
mongoose.connection.on('error', err => {
console.log(`DB connection error: ${err.message}`)
});
//middlewares
app.use(morgan('dev'))
app.use(bodyParser.json())
app.use(cookieParser());
app.use(expressValidator());
//routes middleware
app.use("/api", authRoutes);
app.use("/api", userRoutes);
app.use("/api", categoryRoutes);
// app.get('/',(req, res)=>{
// res.send('hello from node updated');
// })
const port = process.env.PORT || 8000
app.listen(port, () => {
console.log(`Servver is running on port ${port}`)
});
[1]: https://i.stack.imgur.com/vKqBt.png
只需在您的路线中使用 category/create/:userId
我正在学习 MERN 开发,并尝试创建 API 以使用邮递员添加类别。但是低于错误
Cannot POST /api/category/create/62545ea650265cda5b08f10d
我在此处添加邮递员的代码和屏幕截图,如有任何帮助,我将不胜感激!
controller\category.js
const Category = require("../models/category");
//const { errorHandler } = require("../helpers/dbErrorHandler");
exports.create = (req, res) => {
const category = new Category(req.body);
category.save((err, data) => {
if (err) {
return res.status(400).json({
// error: errorHandler(err)
});
}
res.json({ data });
})
}
model\category.js
const mongoose = require('mongoose');
const categorySchema = new mongoose.Schema({
name: {
type: String,
trim: true,
required: true,
maxlength: 32
}
}, { timestamps: true }
);
module.exports = mongoose.model("Category", categorySchema);
routes/category.js
const express = require('express');
const router = express.Router();
const { create } = require('../controllers/category');
const { requireSignin, isAuth, isAdmin } = require('../controllers/auth');
const { userById } = require('../controllers/user');
router.post("/category/create:userId", requireSignin, isAuth, isAdmin, create);
router.param('userId', userById);
module.exports = router;
app.js
// const express = require('express')
// const app = express()
// require('dotenv').config()
// app.get('/',(req, res)=>{
// res.send('hello from node updated');
// })
// const port = process.env.PORT || 8000
// app.listen(port, ()=>{
// console.log(`Servver is running on port ${port}`)
// });
const express = require('express')
// import mongoose
const mongoose = require('mongoose');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const expressValidator = require('express-validator');
// load env variables
const dotenv = require('dotenv');
dotenv.config();
//import routes
const authRoutes = require('./routes/auth');
const userRoutes = require('./routes/user');
const categoryRoutes = require('./routes/category');
//app
const app = express();
//db connection
mongoose.connect(
process.env.DATABASE,
{
useNewUrlParser: true
}
)
.then(() => console.log('DB Connected'));
mongoose.connection.on('error', err => {
console.log(`DB connection error: ${err.message}`)
});
//middlewares
app.use(morgan('dev'))
app.use(bodyParser.json())
app.use(cookieParser());
app.use(expressValidator());
//routes middleware
app.use("/api", authRoutes);
app.use("/api", userRoutes);
app.use("/api", categoryRoutes);
// app.get('/',(req, res)=>{
// res.send('hello from node updated');
// })
const port = process.env.PORT || 8000
app.listen(port, () => {
console.log(`Servver is running on port ${port}`)
});
[1]: https://i.stack.imgur.com/vKqBt.png
只需在您的路线中使用 category/create/:userId