最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Node.js, parse filename from URL - Stack Overflow

matteradmin15PV0评论

How can I parse a url?

site:8080/someFile.txt?attr=100

or

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

I need to get someFile.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).

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 someFile.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).

Share Improve this question edited Jan 16, 2018 at 13:58 Rytis 1,4451 gold badge19 silver badges39 bronze badges asked Nov 18, 2014 at 23:45 AaronAaron 1,3324 gold badges15 silver badges29 bronze badges 8
  • 1 You should mention all the details from the beginning. - nodejs.org/docs/latest/api/url.html – Cheery Commented Nov 18, 2014 at 23:50
  • 2 Do you mean you neeed to get someFile.txt? – falsetru Commented Nov 18, 2014 at 23:53
  • Yes. It could be any file on server (i.e. Kitty.jpg or order.json). – Aaron Commented Nov 18, 2014 at 23:54
  • Is the URL guaranteed to be in this format? Will there ever be a leading http:// – Cory Danielson Commented Nov 18, 2014 at 23:57
  • No that isn't necessary. – Aaron Commented Nov 18, 2014 at 23:59
 |  Show 3 more comments

4 Answers 4

Reset to default 95

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));

here's my working code, hope it helps

import path from 'path'

const getBasenameFormUrl = (urlStr) => {
    const url = new URL(urlStr)
    return path.basename(url.pathname)
}
decodeURIComponent(path.basename('http://localhost/node-v18.12.1-x64.msi'))

node-v18.12.1-x64.msi

Your example can easily be dealt with using Node.js’s url module:

var URL = require('url').parse('site.com:8080/someFile.txt?attr=100');
console.log(URL.pathname.replace(/(^\/|\/$)/g,'')); // "someFile.txt"

However, this doesn’t work with Node.js’s exemplary URL (’cause it’s got more path).

By truncanting the complete path starting at its rightmost slash, it’ll yield the file name:

var URL = require('url').parse('http://user:[email protected]:8080/p/a/t/h?query=string#hash');
console.log(URL.pathname.substring(URL.pathname.lastIndexOf('/')+1)); // "h"

And if that idea is considered safe enough for the appliance, we can do it plain:

var file = url.substring(url.lastIndexOf('/')+1).replace(/((\?|#).*)?$/,'');
                              /* hashes and query strings ----^  */

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far