UNPKG

3.15 kBJavaScriptView Raw
1'use strict';
2
3const fs=require('fs');
4
5const passedArgs = process.argv.splice(2),
6 argCount = passedArgs.length,
7 args = {};
8
9const defaults = {
10 port : 8080,
11 root : process.cwd(),
12 domain : '0.0.0.0',
13 index : 'index.html',
14 log : false
15};
16
17for(let i=0; i<argCount; i++){
18 const data=passedArgs[i].split('=');
19 args[data[0]]=data[1];
20}
21
22// # Config Class
23//
24// [Detailed default config docs and explination](https://github.com/RIAEvangelist/node-http-server/#config-class)
25//
26class Config{
27 constructor(userConfig){
28 Object.assign(this,defaultConfigs);
29
30 if(userConfig){
31 for(const k in userConfig){
32 this[k]=userConfig[k];
33 }
34 }
35 }
36}
37
38// ### Default node HTTP server config values
39//
40// All of these can be modified and passed into ` new server.Server(myConfigs) ` or ` server.deploy(myConfigs) `
41//
42// ```javascript
43//
44// const myConfig=new server.Config;
45// myConfig.verbose=true;
46// myConfig.port=9922;
47//
48// const myServer=new server.Server(config);
49// myServer.deploy();
50//
51// //or more basically
52// server.deploy({port:9922,verbose:true});
53//
54// ```
55// [Detailed default config docs and explination](https://github.com/RIAEvangelist/node-http-server/#default-node-http-server-configuration)
56//
57//
58const defaultConfigs={
59 verbose : (args.verbose=='true')||false,
60 port : args.port||defaults.port,
61 root : args.root||defaults.root,
62 domain : args.domain||defaults.domain,
63 log : false,
64 logFunction : serverLogging,
65 domains : {
66
67 },
68 server : {
69 index : args.index||defaults.index,
70 noCache : args.noCache=='false' ? false : true,
71 timeout : 30000
72 },
73 https:{
74 ca:'',
75 privateKey:'',
76 certificate:'',
77 passphrase:false,
78 port:443,
79 only:false
80 },
81 contentType : {
82 html : 'text/html',
83 css : 'text/css',
84 js : 'text/javascript',
85 json : 'application/json',
86 txt : 'text/plain',
87 jpeg : 'image/jpeg',
88 jpg : 'image/jpeg',
89 png : 'image/png',
90 gif : 'image/gif',
91 ico : 'image/x-icon',
92 appcache: 'text/cache-manifest'
93 },
94 restrictedType: {
95
96 },
97 errors:{
98 headers : {
99 'Content-Type' : 'text/plain'
100 },
101 404: '404 MIA',
102 415: '415 File type not supported',
103 403: '403 Access Denied',
104 500: '500 {{err}}'
105 }
106};
107
108function serverLogging(data){
109 fs.exists(
110 this.log,
111 function serverLogExsits(exists){
112 data.timestamp=new Date().getTime();
113
114 const JSONData=JSON.stringify(data);
115 let method='appendFile';
116 if(!exists){
117 method='writeFile';
118 }
119 fs[method](
120 this.log,
121 JSONData,
122 function fsMethod(err) {
123 if(err){
124 console.log(err);
125 }
126 }
127 );
128 }.bind(this)
129 );
130}
131
132module.exports=Config;