Created
April 26, 2024 15:01
-
-
Save neogeek/28813fb7dc149811129a7dd79aa033c8 to your computer and use it in GitHub Desktop.
Serve Unity WebGL Locally
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
import http from 'node:http'; | |
import { readFile } from 'node:fs/promises'; | |
import { basename, join } from 'node:path'; | |
const PORT = 8080; | |
http | |
.createServer(async (request, response) => { | |
var filePath = request.url?.replace(/^\//, '') || 'index.html'; | |
var fullPath = join('./Builds/WebGL/', filePath); | |
console.log(`Serving: ${fullPath}`); | |
var extension = basename(fullPath).match(/\..+/); | |
var contentType = 'text/html'; | |
var contentEncoding = ''; | |
if (extension) { | |
switch (extension[0]) { | |
case '.js': | |
contentType = 'text/javascript'; | |
break; | |
case '.css': | |
contentType = 'text/css'; | |
break; | |
case '.json': | |
contentType = 'application/json'; | |
break; | |
case '.png': | |
contentType = 'image/png'; | |
break; | |
case '.jpg': | |
contentType = 'image/jpg'; | |
break; | |
case '.wasm.gz': | |
contentType = 'application/wasm'; | |
contentEncoding = 'gzip'; | |
break; | |
case '.data.gz': | |
case '.framework.js.gz': | |
case '.loader.gz': | |
contentType = 'application/javascript'; | |
contentEncoding = 'gzip'; | |
break; | |
} | |
} | |
try { | |
const contents = await readFile(fullPath); | |
response.writeHead(200, { | |
'Content-Type': contentType, | |
'Content-Encoding': contentEncoding, | |
}); | |
response.end(contents, 'utf-8'); | |
} catch (error) { | |
console.log(error); | |
} | |
}) | |
.listen(PORT); | |
console.log(`Server running at http://localhost:${PORT}/`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment