【问题标题】:Can't start a "curl:localhost:3000" port, shows URI Error无法启动“curl:localhost:3000”端口,显示 URI 错误
【发布时间】:2023-03-31 04:45:02
【问题描述】:

我刚刚开始使用 node js,特别是启动一个服务器,并尝试一个新的应用程序来修剪一个 URL。但是我被困在这里了。

环境:Windows

文本编辑器:VSCode

我的 index.js 代码:

/*
* Primary file for the API
*
*/
// Dependencies

var http = require('http');
var url = require('url');

// The Server shoudl respond to all requests with a string
var server = http.createServer(function (req, res) {
    // Get URL and Parse it
    var parsedUrl = url.parse(req.url, true);

    //Get the Path
    var path = parsedUrl.pathname;
    var trimmedPath = path.replace(/^\/+|\/+$/g, '');

    //Send the Response
    res.end('Hello World!\n');

    //Log the requst path
    console.log('Request recieved on path: ' + trimmedPath);

});

// Start the server, and have it listen on port 3000
server.listen(3000, function () {
    console.log("The server is listening on port 3000 now");
});

之后,我使用命令启动服务器

node index.js

它启动服务器,然后我打开另一个终端并输入

curl localhost:3000

它给了我以下错误

curl : The URI prefix is not recognized.
At line:1 char:1
+ curl localhost:3000
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotImplemented: (:) [Invoke-WebRequest], NotSupportedException
    + FullyQualifiedErrorId : WebCmdletIEDomNotSupportedException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

【问题讨论】:

  • 这可能是 PowerShell 特有的。通常curl 无需提供URI 前缀即可工作。你可以试试curl http://localhost:3000
  • @TsvetanGanev 感谢这工作,但我仍然不明白为什么需要 http。

标签: javascript node.js curl backend


【解决方案1】:

http://https:// 一起使用。

例如:curl http://localhost:3000

【讨论】:

  • 我有 http 但仍然无法正常工作:与远程服务器的连接失败。使用 test.de 而不是 localhost 可以。
【解决方案2】:

不要单独使用curl,而是将http 添加到URL 以查看此类内容:-

curl http://localhost:3000 (你可以使用你的端口号)

注意:https 不是首选

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 2021-05-24
    • 2021-06-13
    相关资源
    最近更新 更多