UNPKG

rapidgame

Version:

A cross-platform commandline tool that prebuilds cocos2d-x libraries for Windows, Mac, Linux, Android and iOS. Also a game templating system.

143 lines (94 loc) 4.91 kB
<!DOCTYPE html> <html> <head> <title>Server.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="public/stylesheets/normalize.css" /> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div class="container"> <div class="page"> <h2 class="title">RapidGame</h2> <h2 class="contact"></h2> <div class="header"> <h1>Server.js</h1> <div class="toc"><a href="#toc">Jump to Table of Contents</a></div> </div> <blockquote> </blockquote> <p>A simple Node.js game server that:</p> <ol> <li>Sends static files as requested.</li> <li>Provides a basic API to game clients.</li> </ol> <p>Keep it running on your own host using <a href="http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever/">forever</a>. Remember the -g option on install.</p> <h3 id="setup">Setup</h3> <div class='highlight'><pre><span class="hljs-keyword">var</span> config = {}, express = <span class="hljs-built_in">require</span>(<span class="hljs-string">"express"</span>), fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">"fs"</span>), path = <span class="hljs-built_in">require</span>(<span class="hljs-string">"path"</span>), server = express(), port = config.serverPort || <span class="hljs-number">8000</span>, protocolHttp = server.listen(port), self = {}; console.log(<span class="hljs-string">"Started server on port: "</span> + port);</pre></div> <h3 id="static-files">Static Files</h3> <p>Serve static files as requested.</p> <div class='highlight'><pre>server.use(<span class="hljs-string">"/"</span>, express.static(__dirname + <span class="hljs-string">"/../Projects/html/"</span>)); server.use(<span class="hljs-string">"/project.json"</span>, express.static(__dirname + <span class="hljs-string">"/../project.json"</span>)); server.use(<span class="hljs-string">"/lib/"</span>, express.static(__dirname + <span class="hljs-string">"/../lib/"</span>)); server.use(<span class="hljs-string">"/Assets/"</span>, express.static(__dirname + <span class="hljs-string">"/../Assets/"</span>)); server.get(<span class="hljs-string">"/"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(req,res)</span>{</span> res.sendfile(<span class="hljs-string">"index.html"</span>); }); server.get(<span class="hljs-string">"/project.json"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(req,res)</span>{</span> <span class="hljs-keyword">try</span> { res.send(<span class="hljs-built_in">JSON</span>.parse(fs.readFileSync(path.join(__dirname, <span class="hljs-string">".."</span>, <span class="hljs-string">"project.json"</span>)))); } <span class="hljs-keyword">catch</span>(e) { console.log(<span class="hljs-string">"Error reading project.json"</span>); } });</pre></div> <h3 id="public-api">Public API</h3> <p>Provide <code>api/counter</code> which increments the visitor number and returns it.</p> <div class='highlight'><pre>self.counter = <span class="hljs-number">0</span>; server.get(<span class="hljs-string">"/api/counter"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(req,res)</span>{</span> self.counter = self.counter + <span class="hljs-number">1</span>; res.send(self.counter + <span class="hljs-string">""</span>); });</pre></div> <a name="toc">&nbsp;</a> <div id="bottom-toc"> <h3>Table of Contents</h3> <ol> <li> <a class="source" href="README.html"> README.md </a> </li> <li> <a class="source" href="GameScene.html"> GameScene.js </a> </li> <li> <a class="source" href="MenuScene.html"> MenuScene.js </a> </li> <li> <a class="source" href="Game.html"> Game.js </a> </li> <li> <a class="source" href="Server.html"> Server.js </a> </li> </ol> </div> <div class="fleur">h</div> </div> </div> </body> </html>