1、用node.js写后台需要在服务器上干什么?

2、用node.js搭建cocos 游戏服务器


1、用node.js写游戏后台需要在服务器上干什么?

其实用node.js写后台式不需要向向大多数的编程语言那般需要专门的Web服务器作为容器,像是ASP、ASP.NET需要用IIS作为服务器,PHP

需要搭载Apache或Nginx环境等,JSP需要Tomcat服务器。

而node.js只需几行代码就可以构建服务器。

我们的服务器还需要mysql数据库。


2、用node.js搭建cocos游戏服务器。

var express=require('express');

var app=express();//获得对象

app.listen(8080,"本机服务器ip");//如果用的是局域网,输入localhost


这三句代码就完成了服务器对客户端访问8080端口的监听。

在cocos creator

xhr . open ( "GET" ,"服务器ip地址" , true );

xhr . onreadystatechange = function () {
if ( xhr . readyState === 4 && ( xhr . status >= 200 && xhr . status < 300 )){
console . log ( "http res(" + xhr . responseText . length + "):" + xhr . responseText );
try {
var ret = JSON . parse ( xhr . responseText );
if ( handler !== null ){
handler ( ret );
} /* code */
} catch ( e ) {
console . log ( "err:" + e );
//handler(null);
}
}
};

利用ajax来访问服务器

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐