小编Tho*_*lla的帖子

新的URL() - WHATWG URL API

我正在搞乱节点,我正在尝试获取URL类的实例(因为那些方便的属性).喜欢:

const { URL } = require('url');
(...)
http.createServer((request,response) => {
    let uri = new URL(request.url);
    (...)
}
Run Code Online (Sandbox Code Playgroud)

但它失败了

TypeError [ERR_INVALID_URL]: Invalid URL: /
Run Code Online (Sandbox Code Playgroud)

这很有趣因为

const url = require('url');
url.parse();
Run Code Online (Sandbox Code Playgroud)

作品.所以我很好奇.我明白后面的方法比较旧.

我正在本地开发所以发送请求我在浏览器中使用localhost:8000.

如何使用该信息request实例化新的URL对象?

信息和我已经看过的东西:

node -v
v9.3.0

https://nodejs.org/api/url.html#url_class_url
https://stackoverflow.com/questions/44738065/uncaught-typeerror-url-is-not-a-constructor-using-whatwg-url-object-support-for
https://stackoverflow.com/questions/45047840/in-node-js-how-to-get-construct-url-string-from-whatwg-url-including-user-and-pa
https://stackoverflow.com/questions/47481784/does-url-parse-protect-against-in-a-url
https://stackoverflow.com/questions/17184791/node-js-url-parse-and-pathname-property
Run Code Online (Sandbox Code Playgroud)

javascript node.js server

9
推荐指数
1
解决办法
5252
查看次数

在 Node.js 中需要带有注释的 JSON

如果您使用 typescript,您可以初始化默认的 tsconfig.json,该 json 中将包含 javascript // 和 /* */ 注释。我遇到过 ts-jest 的情况,我需要要求我的 tsconfig 并解析它,但 jest 没有解析它,因为 json 不允许注释。看到这个我不确定打字稿如何处理它,但它似乎偏离了规则。

// this fails if tsconfig has comments
const tsconfig = require('./tsconfig')
Run Code Online (Sandbox Code Playgroud)

我想保留这些注释,因为它们确实有助于理解和维护我的 tsconfig.json,并且我希望我的配置能够避免重复代码并使事情更加动态。

有没有办法使用nodejs要求带有注释的json文件?

javascript json node.js typescript json5

4
推荐指数
1
解决办法
3380
查看次数

标签 统计

javascript ×2

node.js ×2

json ×1

json5 ×1

server ×1

typescript ×1