How to use POST , GET for making search-box in Nodejs

10,261

Use body-parser.

var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));

router.post('/search/process', function(req, res, next) {

  console.log(req.body);

});

See https://scotch.io/tutorials/use-expressjs-to-get-url-and-post-parameters#post-parameters for an example.

Share:
10,261
Jeong Ki Hun
Author by

Jeong Ki Hun

Updated on June 04, 2022

Comments

  • Jeong Ki Hun
    Jeong Ki Hun almost 2 years

    I want to post and get the getparameter keyword from form value and use this query

    SELECT * from cider.cid_contents 
    where con_content like \'%'+ keyword +'%\' 
    order by con_no desc;
    

    But I don't know how to get the keyword value from post or form method. There are codes

    search.js

    var express = require('express');
    var router = express.Router();
    var mysql = require("./model/mysql");
    
    /* GET home page. */
    router.post('/search/process', function(req, res, next) {
    
        var keyword = req.body.keyword;
    
        console.log(keyword);
    
        res.redirect('/search');
    
    
      });
    
    router.get('/search/:keyword', function(req, res, next) {
        var keyword;
        keyword = req.params.keyword;
        console.log("+++++");
        console.log(keyword+"1234");
        mysql.select('SELECT * from cider.cid_contents where con_content like \'%'+ keyword +'%\' order by con_no desc;',
    
    
        function (err, data){
            if (err) throw err;
    
        res.render('front/search/search', { contents : data});
      });
    });
    
    
    module.exports = router;
    

    that's a form tag(top.ejs)

    <form action="/search/process" method="post">
                    <input type="text" class="form-control web-search-box" placeholder="search" name="keyword" value=''></div>
                    <div id="search" class="search col-sm-1"><img src="../../page_imgs/fixed_img/icon_search.png"></div>
                    <input type="submit" value="send">
                    </form>
    

    /search (search.ejs)

    <table class="bordered">
            <thead>
              <tr>
                  <th data-field="no">num</th>
                  <th data-field="title">title</th>
                  <th data-field="date">date</th>
                  <th data-field="viewCount">count</th>
                  <th data-field=""></th>
              </tr>
            </thead>
            <tbody>
    
            <% for(var i = 0; i<contents.length; i++) { %>
                <tr>
    
                    <td><%= contents[i].con_no %></td>
                    <td><a href="/adm/contents/detail/<%= contents[i].con_no %>"><%= contents[i].con_title %></a></td>
                    <td><%= contents[i].con_regDate %></td>
                    <td><%= contents[i].con_viewCount %></td>
                    <td><a href="/adm/contents/delete/<%= contents[i].con_no %>">delete</a></td>
                </tr>
            <% } %> 
            </tbody>
          </table>