Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript (node.js) convert url hash to url with parameters

I have a large static result and I'm trying the following changes:

  • Replace the original domain to another one.
  • Convert url's hash to url with parameters using the post id only for a specific domain (website.com).

This is my original static result example with 3 links and 2 differents domain names:

var json = {This is the static result with many links like this <a href=\"http://website.com/932427/post/something-else/\" target=\"_blank\"> and this is other link obviusly with another post id <a href=\"http://website.com/456543/post/another-something-else/\" target=\"_blank\">, this is another reference from another domain <a href=\"http://onother-website.com/23423/post/please-ingnore-this-domain/\" target=\"_blank\"> }

So, the originals url's I need to change are two, according with the above example:

http://website.com/932427/post/something-else/ 
http://website.com/456542/post/another-something-else/

And I want to change that links now with this format:

http://other_domain.com/id?=932427/
http://other_domain.com/id?=456543/

And the final result should look like this into the static result.

By the way I'm using node.js

Thanks in advance

like image 809
user3368141 Avatar asked Dec 07 '25 02:12

user3368141


1 Answers

Node.js has a built in module for parsing and constructing URLs. Your solution can be written as:

var url = require('url'); // Comes with Node.

// Get the path: '/932427/post/something-else/'
var path = url.parse('http://website.com/932427/post/something-else/').path; 

var newUrl = url.format({
    protocol: 'http',
    host: 'other_domain.com',
    query: { id: path.split('/')[1] }
});
like image 109
qubyte Avatar answered Dec 08 '25 16:12

qubyte