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");
});
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 mand
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
Share
Improve this question
asked Feb 6, 2019 at 20:16
Vivek PattanaikVivek Pattanaik
1001 gold badge1 silver badge14 bronze badges
2
-
2
This is probably something which is PowerShell specific. Usually
curl
works without providing the URI prefix. You can try withcurl http://localhost:3000
. – Tsvetan Ganev Commented Feb 6, 2019 at 20:24 - @TsvetanGanev thanks this worked, but I still don't understand why http was needed. – Vivek Pattanaik Commented Feb 8, 2019 at 15:19
2 Answers
Reset to default 8Use with http://
or https://
.
E.g: curl http://localhost:3000
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