Thursday, March 29, 2018

execution of PowerShell scripts

As an Administrator, you can set the execution policy by typing this into your PowerShell window:
Set-ExecutionPolicy RemoteSigned

Wednesday, March 28, 2018

list npm installed packages

For local modules:
npm list --depth=0
For global modules:
npm list -g --depth=0
If you'd like to see all available (remote) versions for a particular module, then do:
npm view <module_name> versions
Note, versions is plural. This will give you the full listing of versions to choose from.
For latest remote version:
npm view <module_name> version  
Note, version is singular.
To find out which packages need to be updated, you can use
npm outdated -g --depth=0
To update global packages, you can use
npm update -g <package>
To update all global packages, you can use:
npm update -g

Monday, March 26, 2018

Node.js module.exports

NodeJS specific feature is when you assign a reference to a new object to exports instead of just adding properties and methods to it like in the last example provided by Jed Watson in this thread. I would personally discourage this practice as this breaks the circular reference support of the CommonJS modules mechanism. It is then not supported by all implementations and Jed example should then be written this way (or a similar one) to provide a more universal module:
(Test.js):
exports.run = function() {
    console.log("Hello World!");
}
(app.js):
var sayHello = require('./Test');
sayHello.run(); // "Hello World!"
Or using ES6 features
(Test.js):
Object.assign(exports, {
    // Put all your public API here
    sayhello() {
        console.log("Hello World!");
    }
});
(app.js):
const { sayHello } = require('./Test');
sayHello(); // "Hello World!"

File server with Nodejs

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',
        '.wav': 'audio/wav',
        '.mp4': 'video/mp4',
        '.woff': 'application/font-woff',
        '.ttf': 'application/font-ttf',
        '.eot': 'application/vnd.ms-fontobject',
        '.otf': 'application/font-otf',
        '.svg': 'application/image/svg+xml'
    };

    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(200, { 'Content-Type': contentType });
                    response.end(content, 'utf-8');
                });
            }
            else {
                response.writeHead(500);
                response.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
                response.end();
            }
        }
        else {
            response.writeHead(200, { 'Content-Type': contentType });
            response.end(content, 'utf-8');
        }
    });

}).listen(8080);