Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

node.js - Add string before URL in NodeJS

I need to add the string json-csv.com/?u= before a URL such as http://api.open-notify.org/iss-now.json with NodeJS.

So far, I have:

var urljoin = require('url-join');
var fullUrl = urljoin('json-csv.com/?u=', 'http://api.open-notify.org/iss-now.json');
console.log(fullUrl);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

from the code snippet you shared it seems like it can be done with basic solution. for example:

var baseUrl = 'http://api.open-notify.org/iss-now.json'
var fullUrl = `json-csv.com/?u=${baseUrl}`

console.log(fullUrl) //===> json-csv.com/?u=http://api.open-notify.org/iss-now.json

let me know if this is the link you would like to have: json-csv.com/?u=http://api.open-notify.org/iss-now.json the base URL you can get from the request if it should be dynamic.


this can be a solution for you

server

app.get('/somepath', ()=>{
 var baseUrl = 'http://api.open-notify.org/iss-now.json'
 var fullUrl = `json-csv.com/?u=${baseUrl}`

 res.status(200).json({url: fullUrl})
})

client


fetch('/somepath')
    .then((response) => {
        return response.json();
    })
    .then((res) => {
        window.open(
            res.url,
            '_blank'
        );
    });


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...