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.
my code:
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.
Without debug:
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.
Debug with inspector:
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 /
Weird
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?