Skip to content

Repository files navigation

simple-http-server

simple-http-server a simple HTTP server is based on NodeJS implemented base features need for debugging and testing javascript files.

Installing globally:

Installation via npm:

 npm install dev-http-server -g

Usage

// add reference to the moduleconstHttpSrv=require('dev-http-server');// create instanceconsthttpSrv=newHttpSrv();// add routehttpSrv.onPost('/page',(req,res)=>{res.writeHead(200,{'Content-Type': 'text/plain'});res.end('Page Two');});// run serverHttpSrv.run({ httpSrv });

If server run correctly, you see following message:

Http Server started on 127.0.0.1:1337

Available Options

HttpSrv.run method has following options

constoption={httpSrv: newHttpSrv(),// instance of HttpSrv objectaddress: '127.0.0.1',// IP address of server
port =1337,// Port of server };

Examples

Static REST response

rest-static-response example

// add reference to the moduleconstHttpSrv=require('dev-http-server');// create instanceconsthttpSrv=newHttpSrv();// add static get response for select2 javascript componenthttpSrv.onGet('/select2',(req,res)=>{res.writeHead(200,{'Content-Type': 'application/json'});constobj={results: [{text: 'Group 1',children: [{id: 1,text: 'element 1'},{id: 2,text: 'element 2'},],},{text: 'Group 2',children: [{id: 3,text: 'element 3'},{id: 4,text: 'element 4'},],},{id: 5,text: 'element 5'},],};res.end(JSON.stringify(obj));});httpSrv.onPost('/page',(req,res)=>{res.writeHead(200,{'Content-Type': 'text/plain'});res.end('Page Two');});// run serverHttpSrv.run({ httpSrv });

Dynamic REST response

rest-dynamic-response

// add reference to the moduleconstHttpSrv=require('dev-http-server');// create instanceconsthttpSrv=newHttpSrv();// add for url /test all possible CRUD operation// GET, POST PUT DELETE// for GET /test server will return all content of file db.json// GET /test/1 return object with id=1// PUT add new object into db// POST update exist record in db.jsonhttpSrv.setJson('/test','src/db.json','id',false);// run serverHttpSrv.run({ httpSrv });

Static content

static-content

// add reference to the moduleconstHttpSrv=require('dev-http-server');// create instanceconsthttpSrv=newHttpSrv();// for GET / server will return content of file index.html in folder /www// GET /file.html return content of file.html in folder /wwwhttpSrv.setStatic('/','/www','index.html');// run serverHttpSrv.run({ httpSrv });

About

Simple Http server base on NodeJS implemented base features for debugging and testing javascript

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages