Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

A simple static file server built with Node.js

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 :: array.reverse 
Javascript :: moment is date equals 
Javascript :: passport local mongoose 
Javascript :: check object is empty javascript 
Javascript :: react conditional class 
Javascript :: val jquery 
Javascript :: sentry ignore errors 
Javascript :: javascript check string lenght 
Javascript :: javascriopt initialize 2d array with size 
Javascript :: time complexity javascript 
Javascript :: npm config proxy 
Javascript :: how to create a cookie in javascript 
Javascript :: react native build android 
Javascript :: url query example 
Javascript :: create loop to specific length react 
Javascript :: return promise in node js 
Javascript :: Conditionallu inline styling in react 
Javascript :: How to Close a React Native Modal with a Button 
Javascript :: js day monday tuesday wednesday 
Javascript :: auto import vscode not working 
Javascript :: Rounding Up To The Nearest Hundred js 
Javascript :: change text in javascript 
Javascript :: generator function fibonacci 
Javascript :: js import and export 
Javascript :: tsconfig build only files and not src 
Javascript :: mongoose express js require 
Javascript :: ReactJS Axios Delete Request Code Example 
Javascript :: get array of selected options from select element 
Javascript :: popup in browser js 
Javascript :: sql how to query data json that store in field 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =