我如何在node.js中搜索?

时间:2022-10-30 21:27:29

I'm using mongoosastic and it's working fine but the problem that I'm facing is that, how do I take the object from .post method and pass it to .get method?

我正在使用mongoosastic它工作正常,但我面临的问题是,如何从.post方法获取对象并将其传递给.get方法?

For example:

router.post('/search', function(req, res, next) {
   Product.search({ something: 'Something'}, function(err, result) {
      if (err) return next(err);
      res.redirect('/search') // <--- How do i pass the result object?
   });
});

router.get('/search', function(req, res, next) {
    res.render('search');
});

I tried another approached where I use req.flash, but I don't really like that approach.

我尝试了另一个接近我使用req.flash的地方,但我真的不喜欢这种方法。

How did you guys solve this problem? it's a really basic search, where user search then it will redirect that user to another page where it either will show the found result or not found.

你们是怎么解决这个问题的?这是一个非常基本的搜索,用户搜索然后它会将该用户重定向到另一个页面,在那里它将显示找到的结果或找不到。

1 个解决方案

#1


2  

You don't need to redirect the user to another route with GET to send the response.

您无需将用户重定向到另一个使用GET的路由来发送响应。

You can serve the request in .post and it is perfectly acceptable.

您可以在.post中提供请求,这是完全可以接受的。

POST and GET are two forms of HTTP Request. No matter what type of request comes to a web server, the response could be anything. It could be a redirect, or an actual web-page, or other types of things such as errors.

POST和GET是两种形式的HTTP请求。无论何种类型的请求进入Web服务器,响应都可以是任何内容。它可以是重定向,也可以是实际的网页,或其他类型的错误。

I don't think you need this, but just to be complete, for search pages it could be a different scenario. GET requests could be bookmarked in the browser, because all it takes to re-render the page is the URL. But POST requests could not be, because it needs post parameters as well which is in the request's body. If you want to let the users bookmark the page with the result or have a permanent link to the same page with the result, you could serve the request in GET requests (as well). Adding an extra parameter like ?q=search-term to the URL for example ...

我不认为你需要这个,但只是为了完整,对于搜索页面,它可能是一个不同的场景。 GET请求可以在浏览器中添加书签,因为重新呈现页面所需的只是URL。但是POST请求不可能,因为它需要post参数,这也是请求的主体。如果您希望让用户使用结果为页面添加书签,或者将结果永久链接到同一页面,则可以在GET请求中提供请求(同样)。在URL中添加一个额外的参数,例如?q = search-term,例如......

This is a sort of sending parameters via GET request. A /search route will also catch a /search?q=search-term URL. You can have access to that using req.query.q and its value would be "search-term" (look at this question for more info). So you can either modify your form to send a GET request instead of POST (<form action="get">...) or you can redirect the user back to the search page using GET and pass the parameter along the URL. And finally serve all the search result in a GET request.

这是一种通过GET请求发送参数。 /搜索路由还会捕获/ search?q =搜索词网址。您可以使用req.query.q访问它,它的值将是“search-term”(请查看此问题以获取更多信息)。因此,您可以修改表单以发送GET请求而不是POST(

...),或者您可以使用GET将用户重定向回搜索页面并沿URL传递参数。最后在GET请求中提供所有搜索结果。

But again, this is more advanced stuff, for what you need to do, generally, it is all good to serve a request whether it is POST or GET or anything else.

但同样,这是更高级的东西,对于你需要做的事情,通常,无论是POST还是GET或其他任何东西,都可以提供服务。

#1


2  

You don't need to redirect the user to another route with GET to send the response.

您无需将用户重定向到另一个使用GET的路由来发送响应。

You can serve the request in .post and it is perfectly acceptable.

您可以在.post中提供请求,这是完全可以接受的。

POST and GET are two forms of HTTP Request. No matter what type of request comes to a web server, the response could be anything. It could be a redirect, or an actual web-page, or other types of things such as errors.

POST和GET是两种形式的HTTP请求。无论何种类型的请求进入Web服务器,响应都可以是任何内容。它可以是重定向,也可以是实际的网页,或其他类型的错误。

I don't think you need this, but just to be complete, for search pages it could be a different scenario. GET requests could be bookmarked in the browser, because all it takes to re-render the page is the URL. But POST requests could not be, because it needs post parameters as well which is in the request's body. If you want to let the users bookmark the page with the result or have a permanent link to the same page with the result, you could serve the request in GET requests (as well). Adding an extra parameter like ?q=search-term to the URL for example ...

我不认为你需要这个,但只是为了完整,对于搜索页面,它可能是一个不同的场景。 GET请求可以在浏览器中添加书签,因为重新呈现页面所需的只是URL。但是POST请求不可能,因为它需要post参数,这也是请求的主体。如果您希望让用户使用结果为页面添加书签,或者将结果永久链接到同一页面,则可以在GET请求中提供请求(同样)。在URL中添加一个额外的参数,例如?q = search-term,例如......

This is a sort of sending parameters via GET request. A /search route will also catch a /search?q=search-term URL. You can have access to that using req.query.q and its value would be "search-term" (look at this question for more info). So you can either modify your form to send a GET request instead of POST (<form action="get">...) or you can redirect the user back to the search page using GET and pass the parameter along the URL. And finally serve all the search result in a GET request.

这是一种通过GET请求发送参数。 /搜索路由还会捕获/ search?q =搜索词网址。您可以使用req.query.q访问它,它的值将是“search-term”(请查看此问题以获取更多信息)。因此,您可以修改表单以发送GET请求而不是POST(

...),或者您可以使用GET将用户重定向回搜索页面并沿URL传递参数。最后在GET请求中提供所有搜索结果。

But again, this is more advanced stuff, for what you need to do, generally, it is all good to serve a request whether it is POST or GET or anything else.

但同样,这是更高级的东西,对于你需要做的事情,通常,无论是POST还是GET或其他任何东西,都可以提供服务。