Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't start a "curl:localhost:3000" port, shows URI Error

I am just starting to begin node js, specifically starting up a server, and trying out a new app to trim out an URL. But I am stuck here.

Environment: Windows

Text Editor: VSCode

my code for 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");
});

After this, I start up the server using the command

node index.js

It shoots up the server and then I open another terminal and I type

curl localhost:3000

and it gives me the following error

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
like image 802
Vivek Pattanaik Avatar asked Feb 06 '19 20:02

Vivek Pattanaik


2 Answers

Use with http:// or https://.

E.g: curl http://localhost:3000

like image 144
Chethan Gowda Avatar answered Sep 21 '22 11:09

Chethan Gowda


Instead of using curl alone, add http to the URL to look something of this type:-

curl http://localhost:3000 (You can use your port number)

NB: https isn't preferred

like image 30
Kimanthi K. Avatar answered Sep 20 '22 11:09

Kimanthi K.