node. JS HTTP Parse Error

Source: Internet
Author: User
Tags parse error

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.