I just wonder how to pick random URLs to performed an axios post request.
I have three servers I want to pick randomly from
JavaScript
x
4
1
let one = "http://127.0.0.1:8000/middle/server_js/"
2
let two = "http://127.0.0.1:8001/middle/server_js/"
3
let three = "http://127.0.0.1:8002/middle/server_js/"
4
currently is using only one:
JavaScript
1
2
1
let req = await axios.post("http://127.0.0.1:8000/middle/server_js/", body, {
2
I just want to choose random port from
Advertisement
Answer
Example of a solution via an array
JavaScript
1
8
1
// Defining an array
2
var arr = ['adress1', 'adress2', 'adress3'];
3
4
// Getting a random array key
5
var rand = Math.floor(Math.random() * arr.length);
6
7
alert(arr[rand]); // "adress2" for example
8