A strange mistake was encountered today.
events.js:72 throw er; //Unhandled ' error ' event ^error:parse Error at socket.socketondata (http.js:1583:20) at tcp.onread (net.js: 527:27)
The code is as follows:
A simple HTTP Server
var http = require (' http '); var server = http.createserver (); Server.listen(N); Server.on (function( Req, res) { res.writehead (' content-type ', ' text/html '); Res.end (' Hello World ') ;
Code for a request server
var http = require (' http '); var req = http.request (' http://localhost:3000 '); Req.on (function(res) { res.setencoding (' UTF8 ') res.on (function(data) { console.log (data) })}) Req.end ();
The problem is in this line of code
Res.writehead (' Content-type ', ' text/html ');
The correct wording is
Res.writehead ($, {' Content-type ': ' text/html '});
If the server side writes the wrong header, the client cannot parse correctly, and the reported error is "Parse error".
The node. JS document explains the error event of the Req object so
If any error was encountered during the request (be-with DNS resolution, TCP level errors, or actual HTTP parse errors An ' error ' event was emitted on the returned request object.
If the HTTP module of node. JS does not resolve the HTTP response, an error event is triggered, which is why the error message has the "Unhandled ' Error ' event".
In the process of checking this bug, one of the things that puzzles me is that the HTTP request code I wrote will go wrong, but it's okay to open http://localhost:3000 with a browser. When you write the wrong code, the program is running normally, so the bug is the most pit.
node. JS HTTP Parse Error