I am new to node. I try to create an HTTP server that receives only POST requests and converts incoming POST body characters to upper-case and returns it to the client. And my server listen on the port provided by the first argument to your program.
this following is my code:
var map = require('through2-map');
var http = require('http');
http.createServer(function (req, res) {
debugger;
console.log("typeof req");
if (req.method === "POST") {
req.pipe(map(function (chunk) {
return chunk.toString().toUpperCase();
})).pipe(res);
}
}).listen(process.argv[2]);
this program expect an port argument from command line.
I run the server: $ node http_uppercaserer.js 8080
And test it: $ curl 127.0.0.1:8080 -X POST -d "asd"
, It return me ASD
that meaning work ok.
But when I use node inspector
to debug my program: $ node-debug http_uppercaserer.js 8080
,
and test it: $ curl 127.0.0.1:8080 -X POST -d "asd"
,
It return me:Cannot POST /
After run node-debug http_uppercaserer.js 8080
, the default break at first line, when I click resume, the terminal will throw me an error:
$ node-debug http_uppercaserer.js 8080
Node Inspector is now available from http://localhost:8080/debug?port=5858
Debugging `http_uppercaserer.js`
debugger listening on port 5858
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:904:11)
at Server._listen2 (net.js:1042:14)
at listen (net.js:1064:10)
at Server.listen (net.js:1138:5)
at Object.<anonymous> (/Users/tangmonk/Documents/learn/nodejs_study/async_and_http/http_uppercaserer.js:12:4)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain [as _onTimeout] (module.js:497:10)
But when If I specify port $ node-debug -p 9000 http_uppercaserer.js 8080
, it works ok, why?
node-debug
will by default add --debug-brk
option which will break the code execution on first line. You would have to resume it. So after doing:
node-debug -p 8082 e.js 8081
Open the link in chrome and resume execution before running curl. Or you can do it like this:
node-inspector --web-port=8082
node --debug=5858 /datastorage/rupayan/e.js 8081
then you can normally do
curl 127.0.0.1:8081 -X POST -d "asd"
PS: sorry for confusion I cant run my node-inspector on default 8080. So no need to give 8082, if you can use 8080 port.
Wierd part answer
That is because node-inspector itself hosts a server and the default address for that is 8080. So both your application and node-inspector cannot use it.