Cocos Creator Learn 01 about Cocos Creator A preliminary exploration of the node. JS server through get and post connections

Source: Internet
Author: User

I. Installation considerations for node. js

1. Reference http://www.runoob.com/nodejs/nodejs-install-setup.html

2.node.js If you do not configure the Package.json file, you will receive a warning NPM WARN saveerror enoent:no such files or directory ... Enter NPM init to configure the file

3. If the NPM file is missing, select Repair in the Control Panel, program.

4. Express error occurs, you need to install Express in the folder of the running project

Ii. about Cocos Creator example of communication through the Get method

Cocos Creator:

1Send3:function () {2         varUrl= "http://127.0.0.1:8081/?name=1&password=2";3         varXHR =NewXMLHttpRequest ();4Xhr.onreadystatechange =function () {5             if(Xhr.readystate = = 4 && (xhr.status >= && Xhr.status < 400)) {6                 varResponse =Xhr.responsetext;7 Console.log (response);8             }9         };TenXhr.open ("GET", URL,true); One         //var str={"name": "1", "Password": "2"} A xhr.send (); -},

node. JS:

varExpress = require (' Express ');varApp =Express ();varurl = require (' URL '));varUtil = require (' util ')); App.get (‘/‘,function(req, res) {varparams = Url.parse (Req.url,true). Query;//parse converts the string to an object, req.url= "/?url=123&name=321", true to indicate that the params is {URL: "123", Name: "321"},false means that the params is url=123 &name=321    //res.write ("website name:" + params.name);    //res.write ("\ n");    //res.write ("website URL:" + params.url);    //res.end ();Res.send (' params.name ' +params.name);})varServer = App.listen (8081,function () {    varHost =server.address (). AddressvarPort =server.address (). Port Console.log ("Application instance, Access address is http://%s:%s", host, Port)})

Iii. Examples of Cocos creator communication through the Post method

Cocos Creator:

Sendpostrequest:function () {        varStr= "name=1&password=2"varserverlink= "http://127.0.0.1:8081/"; varXHR =NewXMLHttpRequest (); Xhr.onreadystatechange=function () {            if(Xhr.readystate = = 4 && (xhr.status >= && xhr.status <= 400) {Console.log ("Connection succeeded"); varResponse =Xhr.responsetext;            Console.log (response);        }        }; Xhr.open ("POST", Serverlink);    Xhr.send (str); }

node. JS:

varHTTP = require (' http ');varQueryString = Require (' querystring '));varurl = require (' URL '));varUtil = require (' util ')); Http.createserver (function(req, res) {//Staging request body Information    varBODY = ""; //Request LinkConsole.log (Req.url); //whenever the request body data is received, it is added to the postReq.on (' Data ',function(chunk) {body+ = chunk;//be sure to use + = if body=chunk, because the request favicon.ico,body will be equal to {}Console.log ("Chunk:", Chunk);    }); //After the end event is triggered, the post is parsed into the true POST request format via Querystring.parse and then returned to the client. Req.on (' End ',function () {        //parsing ParametersBODY = Querystring.parse (body);//Deserializes a string into an objectConsole.log ("Body:", body); Res.end ("Body.name=" +body.name+ "password=" +Body.password); });}). Listen (8081);

Reference website:

Http://docs.cocos.com/creator/manual/zh/scripting/network.html

https://zhidao.baidu.com/question/1835966331253667740.html?qbl=relate_question_0&word=cocos%20creator%20% B7%a2%cb%cdpost

Https://www.cnblogs.com/gamedaybyday/p/6637933.html

Cocos Creator Learn 01 about Cocos Creator Connect to node. JS server via get and post

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.