jeudi 11 avril 2019

Node redirection to random link of one array

I have one script in node, and i want to automatically redirect to randoms links that i have in one array, the page is white and doesn't redirect when i put the maths.random function

There is my code :

const http = require('http');

let randURLs = [
    "aaaaa",
    "xxxxx",
    "bbbbb"
];
const server = http.createServer(function(req, res) {
    if (req.method === 'POST') {
       console.log('post method')
    }
    else {
        let randNumber = Math.floor(Math.random()*randURLs.length);
        res.writeHead(301,{Location: 'https://www.test.com/' + 
randURLs[randNumber]});
     }
    res.end();
});

server.listen(4000);
console.log("localhost:1337 ...");

I want to redirect to https://www.test.com/randomValueInMyArray

Thanks, Benjamin




Aucun commentaire:

Enregistrer un commentaire