Giter Site home page Giter Site logo

shuvava / dev-http-server Goto Github PK

View Code? Open in Web Editor NEW
1.0 0.0 0.0 27 KB

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

License: GNU General Public License v3.0

JavaScript 100.00%
test-javascript http-server test-environment

dev-http-server's Introduction

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 module
const HttpSrv = require('dev-http-server');
// create instance
const httpSrv = new HttpSrv();
// add route
httpSrv.onPost('/page', (req, res) => {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Page Two');
});
// run server
HttpSrv.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

const option = {
    httpSrv: new HttpSrv(), // instance of HttpSrv object
    address: '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 module
const HttpSrv = require('dev-http-server');
// create instance
const httpSrv = new HttpSrv();
// add static get response for select2 javascript component
httpSrv.onGet('/select2', (req, res) => {
    res.writeHead(200, { 'Content-Type': 'application/json' });
    const obj = {
        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 server
HttpSrv.run({ httpSrv });

Dynamic REST response

rest-dynamic-response

// add reference to the module
const HttpSrv = require('dev-http-server');
// create instance
const httpSrv = new HttpSrv();

// 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.json
httpSrv.setJson('/test', 'src/db.json', 'id', false);

// run server
HttpSrv.run({ httpSrv });

Static content

static-content

// add reference to the module
const HttpSrv = require('dev-http-server');
// create instance
const httpSrv = new HttpSrv();

// for GET / server will return content of file index.html in folder /www
// GET /file.html return content of file.html in folder /www
httpSrv.setStatic('/', '/www', 'index.html');

// run server
HttpSrv.run({ httpSrv });

dev-http-server's People

Contributors

shuvava avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.