Append parameters to URL - node.js

How can we append the parameters to path URL using axios.get() in react client?
axios.get('/api/order/user/', {
params: {
user_id: 2
}
})
the route defined in the express server is this.
router.route('/user/:user_id').get(//);
This is what return from the above axios.get() code.
GET /api/order/user/?user_id=2
What I want to achieve is something like this.
GET /api/order/user/2
How can this be achieved?

What you want to achieve is path parameter
const url = '/api/order/user/' + user_id;
axios.get(url);
The params property in axios is Query parameter
GET /api/order/user/?user_id=2

Related

NodeJS/ExpressJS : Access URL of the post request

In a project I'm working on, I need to access the parameters of the url in the backend.
I tried this based on what I found on the internet, but it doesnt seem to work.
//url = "/getDocs?num=15"
router.get("/getDocs", function(req,res,next){
console.log(req.body.num);
res.redirect("/documents");
}
Please help
In url it seems you are using query string which can be accessed as: request.query.num
If you are passing only params in url like getDocs/:num value then it can be accessed as: request.params.num
Simple way of doing this is querying the req object with the property name:
router.get("/getDocs", function(req,res,next){
const name = req.query.name;
res.redirect("/documents");
}
If you sending as a part of route, you can use this:
router.get("/getDocs/:id", function(req,res,next){
const id = req.params.id;
res.redirect("/documents");
}
For more details check here.

how can I get an element in a redirect url using node.js

Non-English country, please forgive my spelling mistakes.
For example, I want to first redirect url1(http://localhost:3000/api/song/167278) to url2(http://localhost:4000/api/song/167278) to use url2's api. And url2 will reponse a json file, which can be seen in the postman's panel.
(postman's pannel)
But there maybe a lot of elements, I only want an element in the file, such as data[0].url. How can I return just return the url value (data[0].url in this json) when people access http://localhost:3000/api/song/167278.
I am using express.js now, how can I edit it? Or is there any other methods?
app.get('api/song/:id', async (req, res) => {
try {
const { id } = req.params
url = "http://localhost:4000/api/song/" + id
res.redirect(url)
}
catch (e) {
console.log(e)
}
}
You could either proxy the entire request there or fetch localhost:4000/api/song/1 in your request handler (with something like node-fetch or axios or with node's APIs and send the fields that you want back to the client as json.

Pass URL as query parameter in node js

I am trying to hit the URL http://localhost:3000/analyze/imageurl=https://www.google.com/ from my browser.
However, due to the presence of //, it does not correctly hit the URL, and gives me an error message, Cannot GET /analyze/imageurl=https://www.google.com/
If I get rid of the backquotes as follows, http://localhost:3000/analyze/imageurl=httpswww.google.com/, it does work correctly.
My backend API looks like this
app.get('/analyze/:imageurl', function (req, res) {
console.log('printing image url:' + req.params.imageurl);
}
Is there a way I can pass in the imageurl with backquotes as a query parameter?
You need to encode your URL before pass it on query string, using encodeURIComponent. For example:
var urlParam = encodeURIComponent('https://www.google.com/');
console.log(urlParam); // https%3A%2F%2Fwww.google.com%2F
var url = 'http://localhost:3000/analyze/' + urlParam;
console.log(url); // http://localhost:3000/analyze/https%3A%2F%2Fwww.google.com%2F
// Decode it in API handler
console.log(decodeURIComponent(urlParam)); // https://www.google.com/
encodeURIComponent
One approach could be to use Express' req.query in your route. It would look something like this:
// Client makes a request to server
fetch('http://localhost:3000/analyze?imageurl=https://google.com')
// You are able to receive the value of specified parameter
// from req.query.<your_parameter>
app.get('/analyze', (req, res, next) => {
res.json(req.query.imageurl)
})

How to get the query parameters in nock callback

I want to access the query parameter in nock reply callback.
The request object that is exposed contains the path that has them as a string. But I would like to access them as a map so that I will not have to deal with parsing the string
const scope = nock('http://www.google.com')
.get('/cat-poems')
.reply(function(uri, requestBody) {
console.log('path:', this.req.path)
console.log('headers:', this.req.headers)
// ...
})
I would expect the query params to be a separate map that I can access
Does anyone know of a way to achieve this?
The value of this.req inside a reply function is an instance of a ClientRequest that has been slightly modified.
Unfortunately for your use case, ClientRequest does not provide an easy way to access just the query params. But you do have access to the full path, from which you can parse the query params out.
const nock = require('nock')
const http = require('http')
const url = require('url')
const scope = nock('http://www.google.com')
.get('/cat-poems')
.query(true)
.reply(function(uri, requestBody) {
const parsed = new url.URL(this.req.path, 'http://example.com')
console.log('query params:', parsed.searchParams)
return [200, 'OK']
})
const req = http.get('http://www.google.com/cat-poems?page=12')
// output >> query params: URLSearchParams { 'page' => '12' }
The object being logged is a URLSearchParams instance.
Using the URL constructor is the preferred method over url.parse now, so I've used that for the example. Keep in mind that URL won't parse relative paths alone, it requires an origin, but since you don't care about the host in the end it can be a dummy value (hence the use of "example.com").

Express JS get Params

Who can I get parameters? If I have a get URL in expressjs like this
http://localhost:3000/api/verify-password-reset?auth_token=a944d6141&user_email=example#gmail.com
I got the first value auth_token using req.params
router.get('/verify-password-reset', (req, res) => {
console.log(req.query.auth_token);
console.log(req.query.email);
}
But email is undefined.
How can I get the email if it has a & in the URL? I tried using docs but couldn't find any solutions.
If you mean params the url looks like this for example: '/example/:id' which id is the the parameter that you specify and you can get its value like this: req.params.id
In your case you are using a QueryString, so you should use this: req.query.user_email which user_email is your key in the url.

Resources