Expressjs and Body-parser Posting Articles Expressjs and Body-parser Posting Articles mongoose mongoose

Expressjs and Body-parser Posting Articles


If you mean, how to refactor your code in a better way, I would suggest these:

1-) use destructuring to parse req.body like this:

app.post("/article/add", function(req, res) {  const { title, author, body } = req.body;  let article = new Article({ title, author, body });  article.save(function(err) {    if (err) {      console.log(err);      return;    } else {      res.redirect("/");    }  });});

2-) use async await syntax:

app.post("/article/add", async function(req, res) {  const { title, author, body } = req.body;  let article = new Article({ title, author, body });  try {    article = await article.save();    res.redirect("/");  } catch (err) {    console.log(err);    return;  }});