From c5ab85ebe67e952785128bd94098e6e1cc8a1463 Mon Sep 17 00:00:00 2001 From: sbosse Date: Sun, 20 Oct 2024 15:09:46 +0200 Subject: [PATCH] Sun 20 Oct 15:08:44 CEST 2024 --- webv86/serve | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 webv86/serve diff --git a/webv86/serve b/webv86/serve new file mode 100644 index 0000000..9f3b7f6 --- /dev/null +++ b/webv86/serve @@ -0,0 +1,65 @@ +#!/usr/bin/env node +var http = require('http'); +var fs = require('fs'); +var path = require('path'); + +http.createServer(function (request, response) { + console.log('request starting...'); + + var filePath = '.' + request.url; + if (filePath == './') + filePath = 'vm86.html'; + filePath='web/'+filePath + + console.log(filePath) + var extname = path.extname(filePath); + var contentType = 'text/html'; + switch (extname) { + 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 '.wav': + contentType = 'audio/wav'; + break; + case '.wasm': + contentType = 'application/wasm'; + break; + case '.html': + contentType = 'text/html'; + break; + } + + fs.readFile(filePath, function(error, content) { + if (error) { + if(error.code == 'ENOENT'){ + fs.readFile('./404.html', function(error, content) { + response.writeHead(200, { 'Content-Type': contentType }); + response.end(content, 'utf-8'); + }); + } + else { + response.writeHead(500); + response.end('Sorry, check with the site admin for error: '+error.code+' ..\n'); + response.end(); + } + } + else { + response.writeHead(200, { 'Content-Type': contentType }); + response.end(content, 'utf-8'); + } + }); + +}).listen(8125); +console.log('Server running at http://127.0.0.1:8125/');