Aaron Aaron - 5 months ago 21
Javascript Question

Node.js, parse filename from URL

How can I parse a url?

site.com:8080/someFile.txt?attr=100


or

site.com:8080/someFile.txt/?attr=100


I need to get
file.txt
, where is a file name I set by myself as the format (txt or some other).

UPDATE

I tried

var path = url.parse(req.url).path;


But I still cannot get the path (
someFile.txt
).

Answer

Something like this..

var url = require("url");
var path = require("path");
var parsed = url.parse("http://example.com:8080/test/someFile.txt/?attr=100");
console.log(path.basename(parsed.pathname));