Socket
Socket
Sign inDemoInstall

node-http-server

Package Overview
Dependencies
Maintainers
4
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-http-server - npm Package Compare versions

Comparing version 6.2.1 to 7.0.0

bin/nodeserver.js

17

example/advanced/basicTemplate.js

@@ -1,5 +0,9 @@

var os = require( 'os' );
var server=require('../../server/http.js');
var config=new server.Config;
const os = require( 'os' );
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
const config=new server.Config;
//set configs
config.verbose=true;

@@ -9,3 +13,3 @@ config.port=8000;

//set listener to process body template
server.beforeServe=beforeServe;

@@ -23,2 +27,4 @@

var interfaceKeys=Object.keys(networkInterfaces);
//generate list items with the network interfaces
for(var i in interfaceKeys){

@@ -39,6 +45,7 @@ serverIPs+='<li><strong>'+interfaceKeys[i]+' : </strong><br>';

//replace the {{some-content}} var in the body with the list created above
body.value=body.value.replace('{{some-content}}',serverIPs);
}
//start the server
server.deploy(config);

@@ -1,8 +0,14 @@

var os = require( 'os' );
var server=require('../../server/http.js');
var config=new server.Config;
const os = require( 'os' );
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
const config=new server.Config;
//setup basic server configs and allow http serving with https serving
config.verbose=true;
config.port=8000;
config.root=__dirname+'/appRoot/';
//add ssl certs and set ssl port
config.https.privateKey = `${__dirname}/../../local-certs/private/server.key`;

@@ -12,7 +18,8 @@ config.https.certificate= `${__dirname}/../../local-certs/server.pub`;

//set listener to process body template
server.beforeServe=beforeServe;
//just before serving process the template
function beforeServe(request,response,body,encoding){
//only parse the /index.html request
//only parse the /index.html request for this example
if(request.url!='/index.html'){

@@ -23,2 +30,3 @@ return;

//dynamically detect available interfaces
//and build content list
var networkInterfaces = os.networkInterfaces();

@@ -42,3 +50,3 @@ var serverIPs='';

//replace {{some-content}} variable with the generated content list
body.value=body.value.replace('{{some-content}}',serverIPs);

@@ -45,0 +53,0 @@ }

@@ -1,7 +0,10 @@

var os = require( 'os' );
var server=require('../../server/http.js');
var config=new server.Config;
const os = require( 'os' );
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
const config=new server.Config;
//setup basic server configs and allow http serving with https serving
config.verbose=true;
config.port=8000;
config.root=__dirname+'/appRoot/';

@@ -11,3 +14,3 @@ config.https.privateKey = `${__dirname}/../../local-certs/private/server.key`;

config.https.port = 4433;
config.https.only = 4433;
config.https.only = true

@@ -14,0 +17,0 @@ server.beforeServe=beforeServe;

var os = require( 'os' );
var server=require('../../server/http.js');
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
var config=new server.Config;

@@ -4,0 +7,0 @@

@@ -1,5 +0,8 @@

var os = require( 'os' );
var server=require('../../server/http.js');
var config=new server.Config;
const os = require( 'os' );
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
const config=new server.Config;
config.verbose=false;

@@ -6,0 +9,0 @@ config.port=8000;

@@ -1,5 +0,8 @@

var os = require( 'os' );
var server=require('../../server/http.js');
var config=new server.Config;
const os = require( 'os' );
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
const config=new server.Config;
config.verbose=false;

@@ -6,0 +9,0 @@ config.port=8000;

@@ -1,5 +0,8 @@

var os = require( 'os' );
var server=require('../../server/http.js');
var config=new server.Config;
const os = require( 'os' );
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
const config=new server.Config;
config.verbose=false;

@@ -6,0 +9,0 @@ config.port=8000;

@@ -1,12 +0,28 @@

var server=require('../../server/http.js');
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
var config=new server.Config;
// instantiate ` config ` as a new ` server.config ` instance so we get all the defaults.
// This will allow us to perform deeper modifications without corrupting the default set.
const config=new server.Config;
//customize the 404 error body
config.errors['404'] = 'These are not the files you are looking for...';
//add support for mp4
config.contentType.mp4 = 'video/mp4';
//set the node server port to 8005
config.port = 8005;
//set the server to be verbose so we can see detailed info on the server, it's requests and responses
config.verbose = true;
//set the root public directory
config.root = __dirname+'/appRoot/';
//start the server
server.deploy(config);
//checkout the server instance
console.log(server);

@@ -1,5 +0,9 @@

var server=require('../../server/http.js');
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
//checkout the server in the console
console.log(server);
//start the server with a config
server.deploy(

@@ -6,0 +10,0 @@ {

@@ -1,3 +0,6 @@

var server=require('../../server/http.js');
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
//start the server with a config
server.deploy(

@@ -11,2 +14,3 @@ {

//checkout the server instance
console.log(server);

@@ -1,18 +0,41 @@

var server=require('../../server/http.js');
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
var config=new server.Config;
// instantiate ` config ` as a new ` server.config ` instance so we get all the defaults.
// This will allow us to perform deeper modifications without corrupting the default set.
const config=new server.Config;
//inspect the config
console.log(config);
//customize the 404 error body
config.errors['404'] = 'These are not the files you are looking for...';
//add support for mp4
config.contentType.mp4 = 'video/mp4';
//set the node server port to 8005
config.port = 8005;
//set the server to be verbose so we can see detailed info on the server, it's requests and responses
config.verbose = true;
//set the root public directory
config.root = __dirname+'/appRoot/';
//set the node ssl privte key
config.https.privateKey = `${__dirname}/../../local-certs/private/server.key`;
//set the node ssl cert
config.https.certificate= `${__dirname}/../../local-certs/server.pub`;
//set the node ssl port
config.https.port = 4433;
//start the server
server.deploy(config);
//take a look at the server instance
console.log(server);

@@ -1,3 +0,6 @@

var server=require('../../server/http.js');
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
//start the server
server.deploy(

@@ -4,0 +7,0 @@ {

@@ -1,3 +0,6 @@

var server=require('../../server/http.js');
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
//start the server
server.deploy(

@@ -16,2 +19,3 @@ {

//checkout the server instance
console.log(server);

@@ -1,3 +0,7 @@

var server=require('../../server/http.js');
var config={
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
//set up a config with multiple domains over ssl
const config={
verbose:true,

@@ -18,4 +22,6 @@ port:8000,

//checkout the multi domain server instance
console.log(server);
//start the server
server.deploy(config);

@@ -1,3 +0,6 @@

var server=require('../../server/http.js');
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
//start the server
server.deploy(

@@ -4,0 +7,0 @@ {

@@ -1,5 +0,9 @@

var server=require('../../server/http.js');
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
//examine the server instance
console.log(server);
//start the server
server.deploy(

@@ -6,0 +10,0 @@ {

@@ -1,3 +0,7 @@

var server=require('../../server/http.js');
var config={
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
//set up a config with multiple domains on just http
const config={
verbose:true,

@@ -13,4 +17,6 @@ port:8000,

//examine the server instance
console.log(server);
//start the server
server.deploy(config);

@@ -1,5 +0,9 @@

var server=require('../../server/http.js');
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
//look at the server instance
console.log(server);
//start the server
server.deploy(

@@ -6,0 +10,0 @@ {

const util = require( 'util' );
const server=require('../../server/http.js');
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
//I am using request for simplicty sake here, you can too.

@@ -4,0 +8,0 @@ const proxy=require('request');

const util = require( 'util' );
const server=require('../../server/http.js');
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
//I am using request for simplicty sake here, you can too.

@@ -4,0 +8,0 @@ const proxy=require('request');

const util = require( 'util' );
const server=require('../../server/http.js');
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
//I am using request for simplicty sake here, you can too.

@@ -10,3 +14,5 @@ const proxy=require('request');

config.https.privateKey = `${__dirname}/../../local-certs/private/server.key`;
config.https.certificate= `${__dirname}/../../local-certs/server.pub`;
config.https.certificate= `${__dirname}/../../local-certs/client.crt`;
config.https.ca= `${__dirname}/../../local-certs/private/rootCA.pem`;
config.https.port = 4433;

@@ -13,0 +19,0 @@

@@ -8,1 +8,3 @@ ## Node HTTP and HTTPS Server examples

The servers in the [basic server folder](https://github.com/RIAEvangelist/node-http-server/tree/master/example/basic) are just static file servers, and the servers in the [advanced server folder](https://github.com/RIAEvangelist/node-http-server/tree/master/example) are dynamic server side rendering examples.
You can see detailed code documentation of the examples on the [node-http-server docs](http://riaevangelist.github.io/node-http-server/) under the examples list.

@@ -6,9 +6,40 @@ # Using TLS and SSL for Secure server

#### important cli commands
- openssl genrsa -out server.key 2048
- openssl req -new -x509 -key server.key -out server.pub -days 365 -config openssl.cnf
- openssl req -new -x509 -key client.key -out client.pub -days 365 -config openssl.cnf
- talk about openssl.cnf edits
### Simple Self Signed Certificates
#### using the local node-ipc certs
```sh
#generate you server key
openssl genrsa -out server.key 2048
#generate the server public key or client cert
openssl req -new -x509 -key server.key -out server.pub -days 365 -config openssl.cnf
```
### Creating Your Own Certificate Authority And Signing Your Cert With it.
```sh
#generate the CA key
openssl genrsa -out rootCA.key 2048
#self sign the CA key
openssl req -x509 -new -nodes -key rootCA.key -sha256 -days 1024 -out rootCA.pem
#generate you server key
openssl genrsa -out server.key 2048
#create your certificate signing request
openssl req -new -key server.key -out server.csr
#generate the server public key or client cert
openssl x509 -req -in server.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out client.crt -days 500 -sha256
```
***need to add info on openssl.cnf edits**
#### using the local certs
This should **ONLY** be done on your local machine. Both the public and private certs are available here on git hub, so its not a good idea to use them over the network.

@@ -15,0 +46,0 @@

{
"name": "node-http-server",
"version": "6.2.1",
"version": "7.0.0",
"description": "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.",

@@ -10,9 +10,17 @@ "main": "server/http.js",

"engines": {
"node": ">=1.0.0"
"node": ">=6.5.0"
},
"bin": {
"nodeserver": "bin/nodeserver.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node ./example/basic/basicApp.js",
"basic": "node ./example/basic/basicApp.js",
"https": "node ./example/basic/https-ONLY-basicApp.js",
"both": "node ./example/basic/https-basicApp.js"
"both": "node ./example/basic/https-basicApp.js",
"template":"node ./example/advanced/basicTemplate.js",
"cluster":"node ./example/basic/cluster-basicApp.js",
"docs": "docco-plus ./README.md ./example/* ./example/**/* ./server/* ./bin/* ./local-certs/* "
},

@@ -42,4 +50,5 @@ "repository": {

"devDependencies": {
"request": "^2.74.0"
}
"docco-plus": "^2.0.0"
},
"dependencies": {}
}

@@ -7,5 +7,11 @@ Node http server with https and proxy support

- ***[TLDR; Quick start examples](#examples)***
- ***[Server Class](#server-class)***
- ***[Config Class](#config-class)***
- ***[Basic use with cluster example](#basic-cluster-server)***
Support for building proxy servers has been added. Documentation coming in next release. [For now see the node-http-server proxy examples](https://github.com/RIAEvangelist/node-http-server/tree/master/example/proxy).
` npm install node-http-server `
` npm i node-http-server `

@@ -23,2 +29,47 @@ [![node-http-server stats](https://nodei.co/npm/node-http-server.png?downloads=true&downloadRank=true&stars=true)](https://www.npmjs.com/package/node-http-server)

[See the code documentation on riaevangelist.github.io](http://riaevangelist.github.io/node-http-server/)
## cli use if you just want to test
Now you can also use the nodeserver cli if you just want to spin up a basic file server to test something out quickly or share on the local network.
```sh
$ sudo npm i -g node-http-server
//start a nodeserver from the current directory on the default port 8080
$ nodeserver
```
You can modify any of the config keys by passing their key value pairs as args.
```sh
//start a verbose nodeserver from the current directory on port 1942
$ nodeserver port=1942 verbose=true
```
### quick npm script tests and examples
```sh
#node ./example/basic/basicApp.js
npm run basic
#node ./example/basic/https-ONLY-basicApp.js
npm run https
#node ./example/basic/https-basicApp.js
npm run both
#node ./example/advanced/basicTemplate.js
npm run template
#node ./example/basic/cluster-basicApp.js
npm run cluster
```
## writing a node http or https server

@@ -30,4 +81,6 @@

var server=require('node-http-server');
const server=require('node-http-server');
server.deploy();
```

@@ -39,18 +92,271 @@

var Server=require('node-http-server').Server;
const Server=require('node-http-server').Server;
class MyCustomServer extends Server{
constructor(){
super();
}
}
const server=new MyCustomServer;
server.deploy();
```
|Server Method| params | description |
|-------------|--------|-------------|
|deploy| config obj (optional) | starts the server. if a config object is passed it will shallow merge it with a clean instantion of the Config class|
|onRequest| request obj | called when request recieved |
|beforeServe|request obj, response obj, body obj, encoding obj| called just before data is served to the client |
|afterServe|request obj| called once data has been fully sent to client |
|Config| config object (optional) | This is a refrence to the Default Config class. Use it to generate a complete config file based off of the default values and arguments passed in when launching the app. Will perform a shallow merge of default values and passed values ig config object passed.|
|Server| none | This is a refrence to the Server Class. Use it to start multiple servers on different ports or to extend the node-http-server.|
## Server Class
|Server Method or member | params | returns / should return | description |
|------------------------------|---------------------------------------------------------------------|----------------------------------|-------------|
|[deploy](#deploy) | `userConfig` obj (optional), `readyCallback` fn (optional) | returns void | Starts the server. if a config object is passed it will shallow merge it with a clean instantion of the Config class. |
|[onRawRequest](#onrawrequest) | `request` obj, `response` obj, `serve` fn | should return true,false or void | Called immediately upon reciept of http(s) request. Called before any request parsing, useful for ` proxy servers ` and request modification, high speed handling, or rejection. Mildly more complex to work with because the request object has not been parsed and decorated with helper members. If this function returns true, the servers response lifecycle will be exited and you must manually call serve. this allows manual immediate and manual async serving. use the ` serve ` argument, ` server.serve ` or ` server.serveFile ` to manually serve the response. |
|[onRequest](#onrequest) | `request` obj, `response` obj, `serve` fn | should return true,false or void | Called when request received. If this function returns true, the servers response lifecycle will be exited and you must manually call serve. this allows manual immediate and manual async serving. use the ` serve ` argument, ` server.serve ` or ` server.serveFile ` to manually serve the response. |
|[beforeServe](#beforeserve) |`request` obj, `response` obj, `body` obj, `encoding` obj, `serve` fn| should return true,false or void | Called just before data is served to the client. If this function returns true, the servers response lifecycle will be exited and you must manually call serve. this allows manual immediate and manual async serving. use the ` serve ` argument, ` server.serve ` or ` server.serveFile ` to manually serve the response. |
|[afterServe](#afterserve) |`request` obj | void | Called once data has been fully sent to client. |
|[Config](#config-class) | n/a | n/a | This is a reference to the Default Config class. Use it to generate a complete config file based off of the default values and arguments passed in when launching the app. Will perform a shallow merge of default values and passed values if a config object passed.|
|[Server](#server-class) | n/a | n/a | This is a reference to the Server Class. Use it to start multiple servers on different ports or to extend the node-http-server.|
---
### [Server Methods](http://riaevangelist.github.io/node-http-server/server/Server.js.html)
#### deploy
` server.deploy ` starts the server.
` server.deploy(userConfig,readyCallback) `
|method | returns |
|--------|---------|
| deploy | void |
| parameter | required | description |
|---------------|----------|-------------|
| userConfig | no | if a ` userConfig ` object is passed it will decorate the [Config class](http://riaevangelist.github.io/node-http-server/server/Config.js.html) |
| readyCallback | no | called once the server is started |
```javascript
const server=require('node-http-server');
server.deploy(
{
port:8000,
root:'~/myApp/'
},
serverReady
);
server.deploy(
{
port:8888,
root:'~/myOtherApp/'
},
serverReady
);
function serverReady(server){
console.log( `Server on port ${server.config.port} is now up`);
}
```
#### onRawRequest
` server.onRawRequest `
` server.onRawRequest(request,response,serve) `
|method | should return |
|--------|---------|
| onRawRequest | bool/void |
| parameter | description |
|------------|-------------|
| request | http(s) request obj |
| response | http(s) response obj |
| serve | ref to ` server.serve ` |
```javascript
const server=require('node-http-server');
const config=new server.Config;
config.port=8000;
server.onRawRequest=gotRequest;
server.deploy(config);
function gotRequest(request,response,serve){
console.log(request.uri,request.headers);
serve(
request,
response,
JSON.stringify(
{
uri:request.uri,
headers:request.headers
}
)
);
return true;
}
```
#### onRequest
` server.onRequest `
` server.onRequest(request,response,serve) `
|method | should return |
|--------|--------------|
| onRequest | bool/void |
| parameter | description |
|------------|-------------|
| request | http(s) request obj |
| response | http(s) response obj |
| serve | ref to ` server.serve ` |
```javascript
const server=require('node-http-server');
const config=new server.Config;
config.port=8099;
config.verbose=true;
server.onRequest=gotRequest;
server.deploy(config);
function gotRequest(request,response,serve){
//at this point the request is decorated with helper members lets take a look at the query params if there are any.
console.log(request.query,request.uri,request.headers);
//lets only let the requests with a query param of hello go through
if(request.query.hello){
// remember returning false means do not inturrupt the response lifecycle
// and that you will not be manually serving
return false;
}
serve(
request,
response,
JSON.stringify(
{
success:false,
message:'you must have a query param of hello to access the server i.e. /index.html?hello'
uri:request.uri,
query:request.query
}
)
);
//now we let the server know we want it to kill the normal request lifecycle
//because we just completed it by serving above. we could also handle it async style
//and request a meme or something from the web and put that on the page (or something...)
return true;
}
```
#### beforeServe
` server.beforeServe `
` server.beforeServe(request,response,body,encoding,serve) `
|method | should return |
|--------|---------|
| beforeServe | bool/void |
| parameter | description |
|------------|-------------|
| request | http(s) request obj |
| response | http(s) response obj |
| body | response content body RefString |
| encoding | response body encoding RefString |
| serve | ref to ` server.serve ` |
type ` RefString `
|type |keys |description|
|---------|---------|-----------|
|RefString| `value` |a way to allow modifying a string by refrence.|
```javascript
const server=require('node-http-server');
server.beforeServe=beforeServe;
function beforeServe(request,response,body,encoding){
//only parsing html files for this example
if(response.getHeader('Content-Type')!=server.config.contentType.html){
//return void||false to allow response lifecycle to continue as normal
return;
}
const someVariable='this is some variable value';
body.value=body.value.replace('{{someVariable}}',someVariable);
//return void||false to allow response lifecycle to continue as normal
//with modified body content
return;
}
server.deploy(
{
port:8000,
root:`${__dirname}/appRoot/`
}
);
```
#### afterServe
` server.afterServe `
` server.afterServe(request) `
|method | should return |
|------------|---------|
| afterServe | n/a |
| parameter | description |
|------------|-------------|
| request | decorated http(s) request obj |
```javascript
const server=require('node-http-server');
server.afterServe=afterServe;
function afterServe(request){
console.log(`just served ${request.uri}`);
}
server.deploy(
{
port:8075,
root:`${__dirname}/appRoot/`
}
);
```
## Examples

@@ -64,3 +370,3 @@

Detailed examples can be found in the [example folder](https://github.com/RIAEvangelist/node-http-server/tree/master/example) The basic example directory is static http and https file servers and the advanced directory has dynamic server side rendering http and https examples including a benchmark example.
Detailed examples can be found in the [example folder](https://github.com/RIAEvangelist/node-http-server/tree/master/example) or under the example folder on the [node-http-server example docs](http://riaevangelist.github.io/node-http-server/example/readme.md.html) The basic example directory is static http and https file servers and the advanced directory has dynamic server side rendering http and https examples including a benchmark example. The ` proxy ` examples show manual serving and response modification examples.

@@ -71,3 +377,3 @@ # Basic http server example

var server=require('node-http-server');
const server=require('node-http-server');

@@ -87,3 +393,3 @@ server.deploy(

var server=require('node-http-server');
const server=require('node-http-server');

@@ -109,3 +415,3 @@ server.deploy(

var server=require('node-http-server');
const server=require('node-http-server');

@@ -126,2 +432,75 @@ server.deploy(

## Basic cluster server
```javascript
//import the `node-http-server` module
//` const server=require(‘node-http-server’); `
const server=require('../../server/Server.js');
//import cluster
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork node-http-server cluster workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on(
'exit',
(worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
}
);
} else {
server.afterServe=(request)=>{
console.log(`${process.pid} served ${request.uri.path}`);
}
//start server in clustered children
server.deploy(
{
verbose: false,
port: 8000,
root:__dirname+'/appRoot/'
}
);
console.log(`Worker ${process.pid} started listening on ${server.config.port}`);
}
```
## Template filling
```javascript
const server=require('node-http-server');
server.beforeServe=beforeServe;
function beforeServe(request,response,body,encoding){
//only parsing html files for this example
if(response.getHeader('Content-Type')!=server.config.contentType.html){
return;
}
const someVariable='this is some variable value';
body.value=body.value.replace('{{someVariable}}',someVariable);
}
server.deploy(
{
port:8000,
root:`${__dirname}/appRoot/`
}
);
```
---

@@ -135,5 +514,5 @@

var server=require('node-http-server');
const server=require('node-http-server');
var config=new server.Config;
const config=new server.Config;
config.errors['404'] = 'These are not the files you are looking for...';

@@ -153,5 +532,5 @@ config.contentType.mp4 = 'video/mp4';

var server=require('node-http-server');
const server=require('node-http-server');
var config=new server.Config;
const config=new server.Config;
config.errors['404'] = 'These are not the files you are looking for...';

@@ -178,5 +557,5 @@ config.contentType.mp4 = 'video/mp4';

var server=require('node-http-server');
const server=require('node-http-server');
var config={
const config={
port:8010,

@@ -195,32 +574,25 @@ root:__dirname + '/www/myApp/',

## Template filling
## [Config Class](http://riaevangelist.github.io/node-http-server/server/Config.js.html)
```javascript
### Default node HTTP server configuration
var server=require('../../server/http.js');
All of these can be modified and passed into ` new server.Server(myConfigs) ` or ` server.deploy(myConfigs) `
server.beforeServe=beforeServe;
```javascript
function beforeServe(request,response,body,encoding){
//only parsing html files for this example
if(response.getHeader('Content-Type')!=server.config.contentType.html){
return;
}
const myConfig=new server.Config;
myConfig.verbose=true;
myConfig.port=9922;
var someVariable='this is some variable value';
const myServer=new server.Server(config);
myServer.deploy();
body.value=body.value.replace('{{someVariable}}',someVariable);
}
//or more basically
server.deploy({port:9922,verbose:true});
server.deploy(
{
port:8000,
root:__dirname+'/appRoot/'
}
);
```
## Default Node HTTP Server Configuration
#### defaults description
```javascript

@@ -323,5 +695,5 @@

var server=require('node-http-server');
const server=require('node-http-server');
var config={
const config={
port:8000,

@@ -332,5 +704,5 @@ root:__dirna\me+'/appRoot/',

//subdomain
'a.myapp':__dirname+'/appSubDomainRoot/',
'a.myapp':`${__dirname}/appSubDomainRoot/`,
//totally different domain, but also on port 8000
'yourapp.com':__dirname+'/appOtherDomainRoot/'
'yourapp.com':`${__dirname}/appOtherDomainRoot/`
}

@@ -351,34 +723,15 @@ }

var os=require('os');
var Server=require('node-http-server').Server;
const os=require('os');
const Server=require('node-http-server').Server;
//MyAwesomeServer inherits from Server
MyAwesomeServer.prototype = new Server;
//Constructor will extend Server
MyAwesomeServer.constructor = MyAwesomeServer;
class MyAwesomeServer extends Server(){
constructor(){
super();
}
function MyAwesomeServer(){
//extend with some stuff your app needs,
//maybe npm publish your extention with node-http-server as a dependancy?
Object.defineProperties(
this,
{
IP:{
enumerable:true,
get:getIP,
//not settable so just return the IP
set:getIP
}
}
);
//locking down Class structure
//no willy nilly cowboy coding
Object.seal(this);
function getIP(){
var networkInterfaces = os.networkInterfaces();
var serverIPs={};
var interfaceKeys=Object.keys(networkInterfaces);
for(var i in interfaceKeys){
IP(){
const networkInterfaces = os.networkInterfaces();
const serverIPs={};
const interfaceKeys=Object.keys(networkInterfaces);
for(let i in interfaceKeys){
serverIPs[

@@ -388,7 +741,7 @@ interfaceKeys[i]

var interface=networkInterfaces[
const interface=networkInterfaces[
interfaceKeys[i]
];
for(var j in interface){
for(let j in interface){
serverIPs[

@@ -402,3 +755,3 @@ interfaceKeys[i]

return IPs;
return serverIPs;
}

@@ -414,5 +767,5 @@ }

var AwesomeServer=require('MyAwesomeServer');
const AwesomeServer=require('MyAwesomeServer');
var server=new AwesomeServer;
const server=new AwesomeServer;

@@ -419,0 +772,0 @@ server.deploy(

@@ -7,11 +7,12 @@ 'use strict';

argCount = passedArgs.length,
args = {},
defaults = {
port : 8080,
root : process.cwd(),
domain : '0.0.0.0',
index : 'index.html',
log : false
};
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++){

@@ -22,11 +23,38 @@ const data=passedArgs[i].split('=');

/**************************************\
*
* These are the valid basic configs
* that can be passed when deploying
* a server, content types are dynamic
* so you can pass whatever you like
*
* ************************************/
// # 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={

@@ -38,12 +66,5 @@ verbose : (args.verbose=='true')||false,

log : false,
//pass this as config for custom logging
logFunction : serverLogging,
domains : {
/*******************\
* domain : /that/domains/root/dir
*
* for sub domains, specify the whole host i.e. "my.sub.domain"
* you may need to edit your hosts file, cnames or iptable
* domain or my.domain etc. goes to 127.0.0.1 for local development
* *****************/
},

@@ -53,3 +74,3 @@ server : {

noCache : args.noCache=='false' ? false : true,
timeout : 30000 //30 second timeout
timeout : 30000
},

@@ -60,2 +81,3 @@ https:{

certificate:'',
passphrase:false,
port:443,

@@ -115,89 +137,2 @@ only:false

/**************************************\
* Config Class.
* ************************************/
class Config{
constructor(userConfig){
//for backwards compatibility
const config = {};
Object.defineProperties(
config,
{
verbose : {
value:defaultConfigs.verbose,
enumerable:true,
writable:true
},
port : {
value:defaultConfigs.port,
enumerable:true,
writable:true
},
root : {
value:defaultConfigs.root,
enumerable:true,
writable:true
},
domain : {
value:defaultConfigs.domain,
enumerable:true,
writable:true
},
https : {
value:defaultConfigs.https,
enumerable:true,
writable:true
},
log : {
value:defaultConfigs.log,
enumerable:true,
writable:true
},
//pass this as config for custom logging
logFunction : {
value:defaultConfigs.logFunction,
enumerable:true,
writable:true
},
domains : {
value:defaultConfigs.domains,
enumerable:true,
writable:true
},
server : {
value:defaultConfigs.server,
enumerable:true,
writable:true
},
contentType : {
value:defaultConfigs.contentType,
enumerable:true,
writable:true
},
restrictedType: {
value:defaultConfigs.restrictedType,
enumerable:true,
writable:true
},
errors : {
value:defaultConfigs.errors,
enumerable:true,
writable:true
}
}
);
if(userConfig){
for(const k in userConfig){
config[k]=userConfig[k];
}
}
//this is to allow backwards compatibility with configTemplate
return config;
}
}
module.exports=Config;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc