https://github.com/languagexange/pong-node
a pong game with socket.io and express
https://github.com/languagexange/pong-node
express node pnpm socket-io
Last synced: 3 months ago
JSON representation
a pong game with socket.io and express
- Host: GitHub
- URL: https://github.com/languagexange/pong-node
- Owner: LanguageXange
- Created: 2024-02-17T01:39:43.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-02-20T17:55:49.000Z (over 1 year ago)
- Last Synced: 2025-01-08T18:13:09.029Z (5 months ago)
- Topics: express, node, pnpm, socket-io
- Language: JavaScript
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Pong Game with Socket.io & Express
Multiplayer pong game with namespaces and rooms
## Setting Up Socket.IO Server
`pnpm init`
`pnpm add socket.io`
update index.html to include socket.io client script
we can remove computerAI related function in our `script.js`
## Using Socket.io with Express
`pnpm add express`
```
// api.js
const express = require("express");
const app = express();
const path = require("path");
app.use(express.static(path.join(__dirname, "public")));
app.use("/", express.static("index.html"));```
### Change to ES Modules syntax
https://socket.io/docs/v3/server-initialization/#es-modules
### Notes
- We will see the error:
`ReferenceError: __dirname is not defined in ES module scope` when we write this
`app.use(express.static(path.join(__dirname, "public")));`- Solution: https://github.com/nodejs/help/issues/2907#issuecomment-757446568
```
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);```
## To play
`pnpm start`
## Helmet JS
`pnpm add helmet`