How to assign URL query parameter before the last parameter in JavaScript

129
May 16, 2021, at 4:00 PM

I'm trying to build an API Proxy in NodeJS it makes a request to an external 3rd party API. The API endpoint needs a parameter named "pages=(int)" appended to each request. Now this parameter must always be the last parameter in the request and the proxy code I'm using appends the "api_key" parameter as the last parameter.

So when I make a request to localhost/trending/data/day&page=1

It gets parsed like this

/trending/data/day&page=1?api_key=testkey123

How do I make it like this

/trending/data/day?api_key=testkey123&page=1

My Code

index.js

require('dotenv').config(); 
const config = require('./config/config');
const express = require('express');
const request = require('request');
const cors = require('cors');
const app = express();
const port = config.port;
app.use(cors());
app.use('/', function(req, res) {
  let url =  config.apiUrl + req.url;
  let query = config.assignKey(req.query);
  req.pipe(request({ qs: query , uri: url })).pipe(res);
});
app.listen(port, () => {
  console.log(`Listening on port:${port`);
});

config.js

let config = {
    apiUrl: process.env.API_URL,
    apiKeyName: process.env.API_KEY_NAME,
    apiKeyValue: process.env.API_KEY_VALUE,
    port: process.env.PORT || 4000,
    assignKey: assignKey
}
function assignKey(query){
  let obj = {};
  obj[config.apiKeyName] = config.apiKeyValue;
  return Object.assign(query, obj);
}
module.exports = config;
Answer 1

First it should be mentioned that you're using an object to store the query parameters. Before ES2015 the order wasn't specified. Since ES2015 the order for string keys is specified (chronological in the order they were inserted). Since this is Node.js code the older standards shouldn't be a problem.

You can swap the objects:

function assignKey(query){
  let obj = {};
  obj[config.apiKeyName] = config.apiKeyValue;
  return Object.assign(obj, query);
}

The call

const query = { page: 1 };
const obj = { apiKey: 'abc' };
console.log(Object.assign(query, obj));

copies all properties from obj to query and returns query. All properties from obj are after query. If you swap the objects

const query = { page: 1 };
const obj = { apiKey: 'abc' };
console.log(Object.assign(obj, query));

the properties from query are copied to obj and you changed the order.

Usually it's even better to create a new object

const query = { page: 1 };
const obj = { apiKey: 'abc' };
console.log(Object.assign({}, obj, query));

Rent Charter Buses Company
READ ALSO
Same fragment creating on sliding in ViewPager2

Same fragment creating on sliding in ViewPager2

I have 2 fragments that I want to show in my viewpager2 but only one shows in the view pager in both the tabs even though I am passing two fragments in my adapter although titles that I set are visible correctly

132
When to use text/plain instead of text/html?

When to use text/plain instead of text/html?

When is it recommended to use text/plain content type over text/html

45
Getting wrong timezone/date from PHP

Getting wrong timezone/date from PHP

When I echo the date_default_timezone_get(); & phpinfo(); I get Europe/Berlin when I should be getting America/New_York

105
Trying to record webcam video without opening and displaying window python

Trying to record webcam video without opening and displaying window python

Hello I followed a tutorial from https://wwwcodingforentrepreneurs

99