-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src/wire/main.js is based upon the "fileserver" feature of the previous version of this project, homeserver. This commit also includes a default 404 error page under defaults/errors/404.html . API documentation will soon be added.
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>404 Error | Aluminum Wire</title> | ||
</head> | ||
<body> | ||
<h1>404 Error</h1> | ||
<p>The requested resource $requrl$ was not found on this server. Please check the request URL.</p> | ||
<p> | ||
Error Message: $errmessage$<br /> | ||
Error Code: $errcode$<br /> | ||
Error Number: $errno$ | ||
</p> | ||
<hr /> | ||
<p>Aluminum Wire on $osplatform$ ($ostype$) version $osversion$ at port $port$</p> | ||
<div style="text-align: center"> | ||
<img src="/aluminum-internals/logo.svg" alt="Aluminum logo" style="width: 150pt" /> | ||
</div> | ||
</body> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const http = require('http') // For creating an HTTP server | ||
// const https = require('https') // For creating an HTTPS server (capability will be added later) | ||
const fs = require('fs') // For reading files from the server filesystem | ||
const os = require('os') // For retrieving operating system information (for error pages) | ||
const path = require('path') // For manipulating request paths | ||
const mimeTypes = require('mime-types') // For sending the correct Content-Type HTTP header | ||
|
||
const errorPage = fs.readFileSync('../../defaults/errors/404.html', 'utf8') // Default error page content | ||
const logo = fs.readFileSync('../../logo.svg', 'utf8') | ||
|
||
function serverHandler (req, res) { | ||
const filename = path.join(__dirname, '..', '..', '..', path.normalize(req.url)) | ||
const mimetype = mimeTypes.lookup(filename) | ||
const reqURLArray = req.url.split('/') | ||
if (reqURLArray[1] === 'aluminum-internals') { | ||
switch (reqURLArray[2]) { | ||
case 'logo.svg': | ||
res.writeHead(200, { 'Content-Type': 'image/svg+xml' }) | ||
res.write(logo) | ||
return res.end() | ||
default: | ||
res.writeHead(404, { 'Content-Type': 'text/plain' }) | ||
res.write('404 ERROR | NOT FOUND' + | ||
os.EOL + | ||
os.EOL + | ||
'The requested internal Aluminum resource, ' + | ||
reqURLArray.slice(2).join('/') + | ||
', does not exist. Please check the request URL.' + | ||
os.EOL + | ||
'Please note that if you have files in a directory named aluminum-internals, then they will not be served. ' + | ||
'The URL /aluminum-internals is reserved for AluminumJS internal resources.' | ||
) | ||
return res.end() | ||
} | ||
} | ||
fs.readFile(filename, function (err, data) { | ||
if (err) { | ||
res.writeHead(404, { 'Content-Type': 'html' }) | ||
res.write( | ||
errorPage.replace(/\$requrl\$/g, req.url) | ||
.replace(/\$osplatform\$/g, os.platform()) | ||
.replace(/\$ostype\$/g, os.type()) | ||
.replace(/\$osversion\$/g, os.release()) | ||
.replace(/\$port\$/g, '80') | ||
.replace(/\$errcode\$/, err.code) | ||
.replace(/\$errno\$/, err.errno.toString()) | ||
.replace(/\$errmessage\$/, err.message) | ||
) | ||
return res.end() | ||
} | ||
res.writeHead(200, { 'Content-Type': mimetype }) | ||
res.write(data) | ||
return res.end() | ||
} | ||
) | ||
} | ||
|
||
const server = http.createServer(serverHandler) | ||
server.listen(80) |