JSON parsing is required for HTTP request return data in node. js

Source: Internet
Author: User
Tags nodejs websocket

In the process of writing a project, need to use real-time data push demand, so first think of the Nodejs websocket module

Find an example of a chat room on the internet and then change it to a push for your needs

Some of the problems encountered

Return data problem: Due to Nodejs is not very familiar with the use of lazy data processing and so on the PHP back-end processing and then use Nodejs to request and then found in the process of the request back to Arraybuffer and then Baidu found that it is because of the need for JSON format conversion So I found the Json.parse () function to convert, just at the beginning of the time is very useful, but later found that the problem is the data volume is very large when the Json.parse report error

Unexpected end of JSON input

After a variety of searches, it was found at the request.

Req.on (' Data ', function (data) {

Datas = json.parse (data)

})

This process is divided into two times to execute the returned data to cut into the same size of the data, so there will be the resolution of the problem finally adopted solution

let datas = ";

Req.on (' Data ', function (data) {

Datas + = data;

})

Req.on (' End ', function () {

DATAS1 = Json.parse (datas);

})

You'll find it after you've changed.

The final summary: The problem is the reason or I do not know the Nodejs is not professional after all, so record hope will not forget to forget the calculation can also find a solution

JSON parsing is required for HTTP request return data in node. js

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.