In NodeJS/ExpressJS, how should one decide between using request params and query strings?

While I am starting to get a hold of NodeJS programming, I am a bit confused by when to use request params and how their practical use differs from using query strings.

In particular, I can do localhost:8080/:param1 in the URL followed by req.params in the code or localhost:8080/?param1=value in the URL followed by req.query in the code, and get the same outcome – the value of param1. How should I decide when to use which? What is a good use case for either of these approaches?

You use

localhost:8080/param1value

when you are referring to a resource, as in

http://barnesandnoble.com/books/12345/how-to-win-friends

You use

localhost:8080?param1=value

when you are supplying a parameter to a request, as in

http://barnesandnoble.com/search?searchterm=friends

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *