UNPKG

node-http-server

Version:

A very simple and fast http server for node, bash, and spawnable from C, Python etc. It is lightweight and great for embedded solutions as well as everyday development or public facing apps.

142 lines (129 loc) 3.46 kB
'use strict'; const fs=require('fs'); const passedArgs = process.argv.slice(2), argCount = passedArgs.length, args = {}; const defaults = { port : 8080, root : process.cwd(), domain : '0.0.0.0', index : 'index.html', log : false }; for(let i=0; i<argCount; i++){ const data=passedArgs[i].split('='); args[data[0]]=data[1]; } // # Config Class // // [Detailed default config docs and explination](https://github.com/RIAEvangelist/node-http-server/#config-class) // class Config{ constructor(userConfig){ Object.assign(this,defaultConfigs); if(userConfig){ for(const k in userConfig){ this[k]=userConfig[k]; } } } } // ### Default node HTTP server config values // // All of these can be modified and passed into ` new server.Server(myConfigs) ` or ` server.deploy(myConfigs) ` // // ```javascript // // const myConfig=new server.Config; // myConfig.verbose=true; // myConfig.port=9922; // // const myServer=new server.Server(config); // myServer.deploy(); // // //or more basically // server.deploy({port:9922,verbose:true}); // // ``` // [Detailed default config docs and explination](https://github.com/RIAEvangelist/node-http-server/#default-node-http-server-configuration) // // const defaultConfigs={ verbose : (args.verbose=='true')||false, port : args.port||defaults.port, root : args.root||defaults.root, domain : args.domain||defaults.domain, log : false, logFunction : serverLogging, domains : { }, server : { index : args.index||defaults.index, noCache : args.noCache=='false' ? false : true, timeout : 30000 }, https:{ ca:'', privateKey:'', certificate:'', passphrase:false, port:443, only:false }, contentType : { html : 'text/html', css : 'text/css', js : 'text/javascript', mjs : 'text/javascript', cjs : 'text/javascript', json : 'application/json', xml : 'text/xml', txt : 'text/plain', jpeg : 'image/jpeg', jpg : 'image/jpeg', png : 'image/png', gif : 'image/gif', ico : 'image/x-icon', svg : 'image/svg+xml', appcache: 'text/cache-manifest', eot : 'application/vnd.ms-fontobject', otf : 'font/otf', ttf : 'font/ttf', woff : 'font/woff', woff2 : 'font/woff2' }, restrictedType: { }, errors:{ headers : { 'Content-Type' : 'text/plain' }, 404: '404 MIA', 415: '415 File type not supported', 403: '403 Access Denied', 500: '500 {{err}}' } }; function serverLogging(data){ fs.exists( this.log, function serverLogExsits(exists){ data.timestamp=new Date().getTime(); const JSONData=JSON.stringify(data); let method='appendFile'; if(!exists){ method='writeFile'; } fs[method]( this.log, JSONData, function fsMethod(err) { if(err){ console.log(err); } } ); }.bind(this) ); } module.exports=Config;