Files

47 lines
1.3 KiB
JavaScript

const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 3000;
const PUBLIC_DIR = __dirname;
const MIME_TYPES = {
'.html': 'text/html; charset=UTF-8',
'.css': 'text/css; charset=UTF-8',
'.js': 'application/javascript; charset=UTF-8',
'.json': 'application/json; charset=UTF-8',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon'
};
const server = http.createServer((req, res) => {
let reqUrl = req.url.split('?')[0];
let safePath = path.normalize(decodeURIComponent(reqUrl));
if (safePath === '/' || safePath === '\\') {
safePath = '/index.html';
}
let filePath = path.join(PUBLIC_DIR, safePath);
fs.stat(filePath, (err, stats) => {
if (err || !stats.isFile()) {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('<h1>404 Not Found</h1><p><a href="/">Return to Home</a></p>');
return;
}
const ext = path.extname(filePath).toLowerCase();
const contentType = MIME_TYPES[ext] || 'application/octet-stream';
res.writeHead(200, { 'Content-Type': contentType });
fs.createReadStream(filePath).pipe(res);
});
});
server.listen(PORT, () => {
console.log(`CurcuVance BioSciences server running at http://localhost:${PORT}`);
});