Skip to content

Commit

Permalink
Removes http-server
Browse files Browse the repository at this point in the history
  • Loading branch information
klebba committed Feb 2, 2024
1 parent 3f045cb commit 6a43fc9
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 224 deletions.
3 changes: 2 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default [
languageOptions: { globals: globals.browser },
...common,
ignores: [
'server.js',
'test.js',
'demo/react/*',
],
Expand All @@ -44,7 +45,7 @@ export default [
...common,
},
{
files: ['test.js'],
files: ['server.js', 'test.js'],
languageOptions: { globals: { ...globals.browser, ...globals.node } },
...common,
},
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"registry": "https://registry.npmjs.org/"
},
"scripts": {
"start": "http-server -o demo -c-1",
"start": "ROOT=./demo node ./server.js",
"lint": "eslint --max-warnings=0 .",
"lint-fix": "eslint --fix .",
"test": "node test.js | tap-parser -l"
Expand All @@ -28,7 +28,6 @@
"dependencies": {},
"devDependencies": {
"eslint": "^8.56.0",
"http-server": "^14.1.1",
"puppeteer": "^21.10.0",
"tap-parser": "^15.3.1"
}
Expand Down
99 changes: 99 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import http from 'http';
import fs from 'fs';
import path from 'path';

const port = process.env.PORT || 8080;
const root = process.env.ROOT || '.';
const head = process.env.HEAD || null;

const userHeaders = JSON.parse(head);

const mimeLookup = new Map([
['.css', 'text/css'],
['.html', 'text/html'],
['.ico', 'image/x-icon'],
['.js', 'application/javascript'],
['.json', 'application/json'],
['.md', 'text/markdown'],
['.png', 'image/png'],
['.txt', 'text/plain'],
]);

class Server {
static requestListener(request, response) {
if (request.method === 'GET') {
const fileUrl = request.url;
const filePath = path.resolve(`${root}/${fileUrl}`);
const fileExt = path.extname(filePath);
const mimeType = mimeLookup.get(fileExt);

if (fileExt) {
if (mimeType) {
fs.stat(filePath, (error, stats) => {
if (stats) {
Server.sendFile(response, filePath, mimeType, stats.size);
} else {
Server.sendFileNotFound(response);
}
});
} else {
Server.sendUnknownMimeType(response, fileExt);
}
} else {
if (fileUrl.endsWith('/')) {
const directoryIndex = `${filePath}/index.html`;
fs.stat(directoryIndex, (error, stats) => {
if (stats) {
Server.sendFile(response, directoryIndex, 'text/html', stats.size);
} else {
// @TODO pushState optional
Server.sendRootIndex(response);
}
});
} else {
Server.sendRedirect(response, `${fileUrl}/`);
}
}
}
}

static sendUnknownMimeType(response, fileExt) {
const message = `Error 500: Unknown MIME type for file extension: ${fileExt}`;
response.writeHead(500, { 'Content-Type': 'text/plain', 'Content-Length': message.length, ...userHeaders });
response.write(message);
response.end();
}

static sendFileNotFound(response) {
const message = 'Error 404: Resource not found.';
response.writeHead(404, { 'Content-Type': 'text/plain', 'Content-Length': message.length, ...userHeaders });
response.write(message);
response.end();
}

static sendRedirect(response, location) {
response.writeHead(301, { 'Content-Type': 'text/plain', 'Content-Length': 0, location, ...userHeaders });
response.end();
}

static sendFile(response, filePath, mimeType, contentLength) {
response.writeHead(200, { 'Content-Type': mimeType, 'Content-Length': contentLength, ...userHeaders });
fs.createReadStream(filePath).pipe(response);
}

static sendRootIndex(response) {
const rootIndex = `${root}/index.html`;
fs.stat(rootIndex, (error, stats) => {
if (stats) {
Server.sendFile(response, rootIndex, 'text/html', stats.size);
} else {
Server.sendFileNotFound(response);
}
});
}
}

http.createServer(Server.requestListener).listen(port, () => {
// eslint-disable-next-line no-console
console.log(`Development server running: http://localhost:${port}`);
});
Loading

0 comments on commit 6a43fc9

Please sign in to comment.