DEV Community

Zack
Zack

Posted on

2 1

An Online Multiplayer Game Example

Hey everyone! It's been a little while, but here I am with a new project: a simple game server made entirely with Node.

Source code and docs here.

Let's go over the server script:

./server.js

// HTTP Server

var express = require("express"); // Load the express package
var app = express(); // Create an app with express
var up = true; // Unused value... I'll remove it in the future.
var data = { // The cube's data.
    x: 0, // "X" position of the cube.
    y: 0 // "Y" position of the cube.
};

app.get("/updatePos", (req, res) => { // Create a listener for /updatePos
    res.json(data); // Return the cube's data.
});

app.get("/setPos/:x/:y", (req, res) =>  { // ^
    data.x = parseInt(req.params.x); // Sets the "X" position
    data.y = parseInt(req.params.y); // Sets the "Y" position
    res.status(200); // Sets the result status as a success
    res.json({data: "Hello, world!"}); // Just so it returns something.
});

app.listen(8080, () => { // Start listening.
    console.log("Hosted server at localhost:8080"); // Let the user know the server started
});

I explained each line of code briefly with comments so you can understand it a bit better. Also, don't forget to read the readme.md file so you know it's dependencies.

Have a nice day! :D

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️