Bryan
Bryan

Reputation: 1468

How can I differentiate between two POST calls in expressjs?

I currently have two different forms on one inven.ejs file:

One for simple description:

///inven.ejs
  <form method="POST" value="inven">
     <div id="some-form" style="display: none;">
       <table>
         <tr>
           <td><label for="item">Item</label></td>
           <td><input type="text" name="item" required/></td>
         </tr>
         <tr>
           <td><label for="text-box-value">Value</label></td>
         </tr>
         <tr>
           <td><label for="comments">Comments</label></td>
           <td><textarea rows="4" cols="50" required></textarea></td>
         </tr>
         <tr>
           <td><input type="submit" /></td>
         </tr>          
       </table>
     </div>
   </form>

and another for file upload:

///inven.ejs
   <div id="fileUp">
     <form id="fileUpload" name="fileUpload" enctype="multipart/form-data" method="post">
       <fieldset>
          <input type="file"id="fileSelect">
          <input type="submit" name="upload" value="upload">
       </fieldset>
     </form>
   </div>

In express, how can I differentiate between these two posts in my list.js file?

router.post('/list', function(req,res){
   // ???
});

Do I need two routers? Am I completely doing this incorrectly? Thank you!

EDIT: Included an image, if it helps?

Image of what I'm attempting to do here

Upvotes: 2

Views: 723

Answers (1)

alandarev
alandarev

Reputation: 8635

It makes sense to have forms post to a different address.

That is, <form id="fileUpload" action="file_upload" ... > Then, if the form is found at http://server/my_form, it will post to http://server/file_upload

In your Node.js router you need to catch that and done.


If you still desire to send both forms to the same address, you can then use hidden fields.

Example: <input type="hidden" name="form_type" value="file_up_form">. Then in your Node.js you check for the argument form_type and check its value.

Upvotes: 2

Related Questions