Creating a Post - Sending Data

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 344:- Creating a Post - Sending Data

To create a new post, you need to create a form to send the data to the server. Here are the steps to do it:

  1. In the views folder, create a new file called new.ejs. This file will contain the form to create a new post.
  2. Inside the new.ejs file, create a form element with the following attributes:

htmlCopy code

<form method="POST" action="/posts"> </form>

This form will send a POST request to the /posts route when the user submits it.

  1. Add input fields to the form for the title and body of the post. For example:

htmlCopy code

<form method="POST" action="/posts">  <div>    <label for="title">Title</label>    <input type="text" id="title" name="title" required>  </div>  <div>    <label for="body">Body</label>    <textarea id="body" name="body" rows="10" required></textarea>  </div>  <button type="submit">Create Post</button> </form>

This form has two input fields: a text field for the title of the post, and a textarea for the body of the post. The name attribute of each input field corresponds to the property of the post object in the database.

  1. In the routes/posts.js file, add a new route to handle the POST request from the form:

javascriptCopy code

router.post('/', isLoggedIn, async (req, res) => {  // create a new post using the data from the form  const post = new Post({    title: req.body.title,    body: req.body.body,    author: req.user._id  });  // save the new post to the database  await post.save();  // redirect to the home page  req.flash('success', 'Post created successfully!');  res.redirect('/'); });

This route uses the Post model to create a new post object using the data from the form. The author property is set to the ID of the currently logged-in user. The new post is saved to the database using the save() method, which is an asynchronous operation. Once the post is saved, the user is redirected to the home page, and a success flash message is displayed.

  1. Finally, in the app.js file, add the body-parser middleware to parse the data from the form:

javascriptCopy code

const bodyParser = require('body-parser'); // parse application/x-www-form-urlencoded app.use(bodyParser.urlencoded({ extended: false })); // parse application/json app.use(bodyParser.json());

This middleware parses the request body and makes it available in the req.body object.

With these steps, you have created a form to create a new post and handled the POST request to save the post to the database.

40. Converting To Ajax

2 Comments

@niteshguptav63
niteshguptav63 Nov 17, 2024 at 1:39 PM

I am not able to access videos from second class and further. I have already completed first class

@niteshguptav63
niteshguptav63 Nov 16, 2024 at 10:56 AM

When will I get my course?

@admin79
admin79 Nov 17, 2024 at 1:29 PM

Now, Your query was resolved.

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support