javascript - How can I create search result page on example.comskeyword? - Stack Overflow

app.jsapp.get('results',showResult)var express = require('express')var n = req.q

app.js

app.get('/results',showResult)
var express = require('express')
var n = req.query.query;
mysql_crawl.query('SELECT prod_name, full_price FROM `xxx` WHERE MATCH(data_index) AGAINST("'+n+'")', function(error, product_data) {
res.render('result.html', {result: n, related: product_data: product_data})
        });

result.html

<form action= '/results' class="input-group input-group-lg" id="search">
         <input type="text" class="input-group form-control"  name="query" value='<%= result %>'/>
               <span class="input-group-btn">
                     <button class="btn btn-secondary" type='button'>Search</button>
               </span>
</form>

When I search, I get a URL like example/results?query=keyword, but I want a results page at example/s/keyword.

Both pages should be the same results. If I change the keyword in the URL, it should show search results for that keyword.

So my question is, how do I create the example/s/keyword page to duplicate the results at example/results?query=keyword?

app.js

app.get('/results',showResult)
var express = require('express')
var n = req.query.query;
mysql_crawl.query('SELECT prod_name, full_price FROM `xxx` WHERE MATCH(data_index) AGAINST("'+n+'")', function(error, product_data) {
res.render('result.html', {result: n, related: product_data: product_data})
        });

result.html

<form action= '/results' class="input-group input-group-lg" id="search">
         <input type="text" class="input-group form-control"  name="query" value='<%= result %>'/>
               <span class="input-group-btn">
                     <button class="btn btn-secondary" type='button'>Search</button>
               </span>
</form>

When I search, I get a URL like example./results?query=keyword, but I want a results page at example./s/keyword.

Both pages should be the same results. If I change the keyword in the URL, it should show search results for that keyword.

So my question is, how do I create the example./s/keyword page to duplicate the results at example./results?query=keyword?

Share Improve this question edited Jul 28, 2017 at 8:54 TRiG 10.7k9 gold badges61 silver badges111 bronze badges asked May 2, 2017 at 7:08 Moomoo SosoMoomoo Soso 991 gold badge1 silver badge7 bronze badges 1
  • use the express request params. – Matt Greenberg Commented May 2, 2017 at 7:15
Add a ment  | 

2 Answers 2

Reset to default 0

You can create new route as

app.get('/s/:keyword', function (req, res) {
    var n = req.params.keyword;
    // do rest of the task
};

Instead of repeating same code in two route, you can create a function then call the same function in both routes.

app.get('/s/price/:sea_query', function (req, res) {
    var n = req.params.sea_query;
};

I can use this to get data from url, thanks :)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745466662a4628944.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信