Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

vanilla js http server

var http = require('http');
var fs = require('fs');
var path = require('path');

http.createServer(function (request, response) {
    console.log('request ', request.url);

    var filePath = '.' + request.url;
    if (filePath == './') {
        filePath = './index.html';
    }

    var extname = String(path.extname(filePath)).toLowerCase();
    var mimeTypes = {
        '.html': 'text/html',
        '.js': 'text/javascript',
        '.css': 'text/css',
        '.json': 'application/json',
        '.png': 'image/png',
        '.jpg': 'image/jpg',
        '.gif': 'image/gif',
        '.svg': 'image/svg+xml',
        '.wav': 'audio/wav',
        '.mp4': 'video/mp4',
        '.woff': 'application/font-woff',
        '.ttf': 'application/font-ttf',
        '.eot': 'application/vnd.ms-fontobject',
        '.otf': 'application/font-otf',
        '.wasm': 'application/wasm'
    };

    var contentType = mimeTypes[extname] || 'application/octet-stream';

    fs.readFile(filePath, function(error, content) {
        if (error) {
            if(error.code == 'ENOENT') {
                fs.readFile('./404.html', function(error, content) {
                    response.writeHead(404, { 'Content-Type': 'text/html' });
                    response.end(content, 'utf-8');
                });
            }
            else {
                response.writeHead(500);
                response.end('Sorry, check with the site admin for error: '+error.code+' ..
');
            }
        }
        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/');
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to use data sets javascrip[t 
Javascript :: use useRef to get current class 
Javascript :: js exclude from object 
Javascript :: how-to-show-base64-image-in-react 
Javascript :: time stamp to date js 
Javascript :: vue directive 
Javascript :: how to check if string is valid jwt 
Javascript :: which line will generate a random number between 1 to 10 javascript 
Javascript :: format iso time in very human readable format js such as n seconds ago etc 
Javascript :: look through object keys javascript 
Javascript :: $out in mongodb 
Javascript :: angular multiselect dropdown 
Javascript :: count occurence in array js 
Javascript :: javascript how to do if else 
Javascript :: jquery animate transform 
Javascript :: How to Return Specific Values from a Filter in Javascript 
Javascript :: sort in array in javascript 
Javascript :: bin to bit string javascript 
Javascript :: address format 
Javascript :: how to loop over an array in js 
Javascript :: select jquery display none 
Javascript :: moment duratuion from hours 
Javascript :: for loop on array in javascript 
Javascript :: prisma bigint 
Javascript :: postmark with nodejs 
Javascript :: check file name in url 
Javascript :: Convert mnemonic to seed in javascript 
Javascript :: how to highlight active screen react native 
Javascript :: Material-ui add box icon 
Javascript :: dropdowndirection 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =