Socket
Socket
Sign inDemoInstall

node-http-server

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-http-server

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.


Version published
Weekly downloads
3K
increased by2.88%
Maintainers
1
Weekly downloads
 
Created
Source

Node http server


Simple to use stand alone node HTTP Server you can spin up from node apps, bash scripts, the commandline, C or python apps etc.

npm install node-http-server

npm info : See npm trends and stats for node-http-server
node-http-server npm version supported node version for node-http-server total npm downloads for node-http-server monthly npm downloads for node-http-server npm licence for node-http-server

GitHub info :
node-http-server GitHub Release GitHub license node-http-server license open issues for node-http-server on GitHub

Package Quality :
node-http-server Package Quality


Defaults


currently modifiable via any interface, commandline, bash, node etc.
port        : 8080
root        : Current Working Directory (where you execute the command from)
domain      : 0.0.0.0
index       : index.html
verbose     : false
noCache     : true
log         : false
logFunction : serverLogging
keydescription
portthe port on which the server should run
rootthe absolute location to the root dir for the public file system
domainthe domain which this server applies to. You can add more servers via the node domains implementation described below than you can via bash or commandline. If you want to accept incoming requests for ANY Applicable Domain use 0.0.0.0 this will allow any request that is pointed at this machine on the specified port to use this server config.
indexthe default file to look for in a dir. if not found a 404 will be displayed
verboseshould the server display detailed info about what it is doing
noCacheshould the server prevent caching
logfull path to log file, if specified file is not present it will be created, however the dir must be there. ie. /tmp/server.log It is recommended that you timestamp this file name with a time stamp like : '~/serverLogs/domain-'+new Date().getTime()+'.log' this will create a new log file each time the server is started/restarted/reboot etc...
logFunctionthis defaults to append timestamp to headers object and log as JSON in the log file. However, you can overwrite this and do whatever you like with the JSON data if you so choose. It accepts a javascript Object as the first argument for parsing. If you manually log to the default function. If you overwrite the function for custom logging, you must accept a javascript object as the first argument for the default log requests to function.

currently modifiable via node
domains     :   {}

contentType :   {
    html    : 'text/html',
    css     : 'text/css',
    js      : 'text/javascript',
    json    : 'application/json',
    txt     : 'text/plain',
    jpeg    : 'image/jpeg',
    jpg     : 'image/jpeg',
    png     : 'image/png',
    gif     : 'image/gif',
    ico     : 'image/x-icon',
    appcache: 'text/cache-manifest'
}

restrictedType: {}

errors  :   {
    headers : {
        'Content-Type' : 'text/plain'
    },
    404: '404 MIA',
    415: '415 File type not supported',
    403: '403 Access Denied',
    500: '500 {{err}}'
}
keydescription
domainsthis is a mapping of hostname to path. It can be used for multiple different domains, or for subdomains.
contentTypemapping of file extension to header content type.
restrictedTypeextensions to which external access will be denied.
errorserror headers and error strings, these can be anything you like from html to text etc. just make sure

Commandline / bash use

launch is an argument that specifies to launch the server now with the provided arguments and defaults

node ~/git/node-http-server/server/http.js root=~/myApp/ port=9999 launch=now

you can specify any of the variables frpom the currently modifiable via any interface, commandline, bash, node etc. section above. The order does not matter.

node ~/git/node-http-server/server/http.js root=~/myApp/ port=8888 verbose=true launch=now

node app use

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

server has 2 methods, deploy and configTemplate

Server Methoddescription
server.configTemplatewill generate a complete config file based off of the default values and arguments passed in when launching the app. DO NOT USE launch=now as an argument for a node app. This will result in launching 2 servers, the one you specify with the arguments passed and then the one the node app launches too.
server.deploywill accept any config params and merge them with a fresh configTemplate, so passing a modified config based off of server.configTemplate() will result in using only the values from the modified config passed when deploying as it will override all of the defaults. The passed config object only merges to one level deep so if you pass a multi level object like contentTypes it will overwrite the default config with what you sent for that object rather than merging your object with the default.

node examples

can be found in the examples folder

basic

this app could be launched as
node basicApp.js verbose=true
to force verbose terminal output. This can be helpful if you have many servers in a single app and want them all to be verbose right now for debugging or testing purposes.

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

console.log(server);

server.deploy(
    {
        port:8000,
        root:'~/myApp/'
    }
);

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

console.log(server);

server.deploy(
    {
        verbose:true,
        port:8001,
        root:'~/myApp/'
    }
);

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

console.log(server);

var config=server.configTemplate();
config.errors['404']    = 'These are not the files you are looking for...';
config.contentType.mp4  = 'video/mp4';
config.port             = 8005;
config.verbose          = true;
config.root             = '~/myApp/'

server.deploy(config);

multiple domains or subdomains
var server=require('node-http-server');

console.log(server);

server.deploy(
    {
        verbose:true,
        port:8010,
        root:process.env.HOME+'/myApp/',
        domain:'myapp',
        domains:{
            'a.myapp':process.env.HOME+'/myApp/mySubdomain/',
            'yourapp.com':process.env.HOME+'/www/yourApp/'
        }
    }
);    

Starting with forever

It is helpful especially when running multiple servers to label them with --uid for easy to remember process names

when starting the same server many times, like every time the system boots you will want to append to the same log file so use -a. Without -a forever will throw an error stating that the log file for the --uid already exists.

forever --uid nodeServer -a start ~/git/node-http-server/server/http.js root=~/myApp/ port=9999 launch=now

This can be set as a .profile command or a .bash_rc command as well if you want to launch the server every time the computer boots up.

Keywords

FAQs

Package last updated on 20 Sep 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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