Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

access to static file nodejs

app.use(express.static(__dirname + '/public'));
<link rel="stylesheet" type="text/css" href="css/style.css" />
Comment

express js static files

app.use(express.static('public'))
Comment

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

access to static file nodejs


app.use(express.static(`${__dirname}/assets`))

Comment

PREVIOUS NEXT
Code Example
Javascript :: download json file react 
Javascript :: javascript set class of element 
Javascript :: js date enlever jour 
Javascript :: browserslisterror contains both .browserslistrc and package.json with browsers 
Javascript :: react native floating button 
Javascript :: check if input is valid js 
Javascript :: boolean object js 
Javascript :: javascript number length 
Javascript :: npm numeral 
Javascript :: add active class and remove active class by click 
Javascript :: data transfer object in node 
Javascript :: link react router dom 
Javascript :: merge objects javascript 
Javascript :: react function being called every minute 
Javascript :: javascript radio button value if checked 
Javascript :: iterate through array js 
Javascript :: react native navigation header right 
Javascript :: validate password with 8 Characters, One Uppercase, One Lowercase, One Number and One Special Case Character 
Javascript :: creating a 2d array in js 
Javascript :: How to get the Class Name of an Object in JavaScript 
Javascript :: loop elements in javascript 
Javascript :: how to get decimal value in js 
Javascript :: modal show with jquery ready function 
Javascript :: JavaScript Use clearInterval() Method 
Javascript :: how to push array into array in angular 
Javascript :: array to object 
Javascript :: react password hashing 
Javascript :: "SyntaxError: Unexpected token o in JSON at position 1 
Javascript :: how to comment in a json file 
Javascript :: mathjax new line 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =