require('url').parse('someurl.com/page')已弃用仅文档,并且我们严格的 linter 对此不满意......我试图在我们的代码中用互联网建议的new URL('someurl.com/page')内容替换它,这在大多数情况下有效。
但是,我们有一些示例,其中 url 是本地图像,some/image.png并且与 url.parse() 配合良好并返回:
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: null,
pathname: '/some/image.png',
path: '/some/image.png',
href: '/some/image.png'
}
Run Code Online (Sandbox Code Playgroud)
但是建议的替换new URL('some/image.png')会引发类型错误...
类型错误 [ERR_INVALID_URL] [ERR_INVALID_URL]:无效的 URL:/some/image.png
url.parse 正在做一些验证并接受本地路径,但新的 url 构造函数没有。该怎么办 ?
我正在读一本名为Node.js 的入门书籍,名为The Node Beginner Book,在下面的代码中(书中给出)我不明白路径名属性在parse方法上的重要性.所以我想知道它在做什么.我不清楚这种方法的文档
var pathname = url.parse(request.url)**.pathname;**
var http = require("http");
var url = require("url");
function start(route, handle) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname; // I don't understand the pathname property
console.log("Request for " + pathname + " received.");
route(handle, pathname);
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
Run Code Online (Sandbox Code Playgroud) 我试图在这里使用WHATWG URL对象支持读取文件
我收到此错误:未捕获TypeError:URL不是构造函数
这是我的代码:
var fs = require("fs");
const { URL } = require('url');
var dbPath = 'file://192.168.5.2/db/db.sqlite';
const fileUrl = new URL(dbPath);Run Code Online (Sandbox Code Playgroud)