javascriptnode.jsexpress.post

i cannot use the router.post method in node.js


i try to use a post request in my node.js application but for some reason that i cannot find it is not working. Perhaps you can help me with this issue

below you can find my report.js and my report.ejs files:

report.js file:

var express = require('express');
var router = express.Router();
const { Pool, Client } = require("pg");
var app = express();
var bodyParser = require("body-parser");
/* GET home page. */

router.use(bodyParser.urlencoded({ extended : false }));

router.get('/', function(req, res, next) {
  res.render('report', { title: 'Express' });
});

router.post('/top', function (req, res) {
  console.log(req.body.todo + " is added to top of the list.");
  res.redirect('/');
});

router.post('/bottom', function (req, res) {
  console.log(req.body.todo + " is added to bottom of the list.");
  res.json({ status: 'success' });
  res.redirect('/');
});

module.exports = router;

report.ejs file:

  <body>
    <form>
      <div class="form-group">
          <label>To Do:</label>
          <input type="text" class="form-control" name="todo">
      </div>
      <button type="submit" class="btn btn-primary btn-lg" formaction="/top"  method="POST">Add to Top</button>
      <button type="submit" class="btn btn-primary btn-lg" formaction="/bottom"  method="POST">Add to Bottom</button>
  </form>
    <script src="/javascripts/reportjs.js"></script>
  </body>
</html>

and i get the error message for both of the buttons:

GET /report 304 10.606 ms - -
GET /stylesheets/report.css 304 2.708 ms - -
GET /javascripts/reportjs.js 304 0.739 ms - -
GET /stylesheets/headder.css 304 1.229 ms - -
GET /bottom?todo=asdasd 404 2.253 ms - 145

Cannot GET /bottom

i dont understand why the application is using a GET method when i use a POST method


Solution

  • The method attribute should be on the <form> tag, not the <button> tag. The default method is GET so that's what the form is doing. Try this:

    <form method="POST">
      <div class="form-group">
        <label>To Do:</label>
        <input type="text" class="form-control" name="todo">
      </div>
      <button type="submit" class="btn btn-primary btn-lg" formaction="/report/top">Add to Top</button>
      <button type="submit" class="btn btn-primary btn-lg" formaction="/report/bottom">Add to Bottom</button>
    </form>
    

    Or you could use formmethod on the <button> tag like this:

    <form>
      <div class="form-group">
        <label>To Do:</label>
        <input type="text" class="form-control" name="todo">
      </div>
      <button type="submit" class="btn btn-primary btn-lg" formaction="/report/top" formmethod="POST"">Add to Top</button>
      <button type="submit" class="btn btn-primary btn-lg" formaction="/report/bottom" formmethod="POST">Add to Bottom</button>
    </form>
    

    Since both buttons use POST it's nicer to put it on the <form> tag and avoid repetition, in my opinion.

    The formaction should start with /report/top or /report/bottom as the current location from the page will be within /report and relative to that. If you use just /top or /bottom it loses context from the /report location.