Setup Server from Scratch with TypeScript
This is a step by step guide for creating a colyseus server with typescript.
Requirements
Setup
Create a new empty directory.
mkdir colyseusServerGo into the directory.
cd colyseusServerInitialise npm with default options
npm initChange the "main" and "scripts" property of the package.json.
{
"main": "dist/main.js",
"scripts": {
"build": "tsc",
"start": "ts-node src/main.ts",
"start:dev": "ts-node-dev --watch \"src/**/*\" --respawn --transpile-only src/main.ts ",
"start:prod": "node dist/main.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
}Install Dependencies
npm i colyseusInstall Dev Dependencies
npm i --save-dev typescript ts-node-devCreate a new file called tsconfig.json in the root of the project
{
"compilerOptions": {
"outDir": "./dist",
"module": "commonjs",
"lib": ["es6"],
"target": "es2016",
"declaration": true,
"removeComments": true,
"noImplicitAny": false,
"experimentalDecorators": true,
"sourceMap": true,
"esModuleInterop": true,
"strict": true,
"allowJs": true,
"strictNullChecks": false,
"forceConsistentCasingInFileNames": true,
"useDefineForClassFields": false
},
"include": [
"src"
]
}Create a new src directory
mkdir srcCreate a new file called main.ts in the src directory
import { Server } from "colyseus"
const port = parseInt(process.env.PORT, 10) || 3000
const gameServer = new Server()
gameServer.listen(port)
console.log(`[GameServer] Listening on Port: ${port}`)Congrats you finished the setup for a colyseus server.
Commands
Now you can start the server with:
npm startFor development you can use the start:dev command. It automatically restarts the server when you change a file.
npm run start:devFor production you first create a build.
npm run buildAfter that you can start the server with the start:prod command. This command uses the files which are created from the build command in the dist folder.
npm run start:prod