如何解决在将数据从表单传递到NodeJS中的URL时遇到频繁错误,该错误无法发布/ urlname
这是我的发件人页面,我在其中发布到URL文章,但出现错误,无法发布POST / URL名称 每次我想保存一篇新文章时,都会出现相同的错误,无法发布/ URL名称
<form action="/routes/articles" method="post">
//here I simply used a partials!
{{>form}}
</form>
this is code where I get data through post method that is mentioned and then it is saved to the database const express = require('express');
const router = express.Router();
const Article = require("../model/schema")
router.get('/',(req,res) => {
res.send('hey !!');
});
router.get('/new',res) => {
res.render('articleGenerator');
});
router.get("/:id",res)=>{
})
router.post("/",async(req,res)=>{
const newArticle = new Article({
title:req.body.title,description:req.body.description,markdown:req.body.markdown
})
//this code saves new article that is send from form page mentioned above
try {
newArticle = await newArticle.save();
res.redirect(`/articles/${newArticle.id}`)
} catch (error) {
res.render("/articleGenerator",{articles:newArticle});
}
})
这是我运行的主文件请注意,我已经导出了一个保存数据的路由articlejs。 module.exports =路由器;
const express = require('express');
const app = express();
const mongoose = require("mongoose");
this is for data base mongoose//
mongoose.connect("mongodb://127.0.0.1:27017/blogging-site",{
useNewUrlParser:true,useUnifiedTopology:true,useCreateIndex:true
})
//here i get the routes of page that is saving the data
const theRoutes = require('./routes/articles')
app.use('/theroutes',theRoutes);
app.set('view engine','hbs');
app.get('/',res) => {
res.render('index');
});
app.listen(3000,() => {
console.log('server is up and running!!');
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。