# dev-http-server

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

Latest version **0.0.5** (published 2017-09-22) · GPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install dev-http-server
pnpm add dev-http-server
yarn add dev-http-server
bun add dev-http-server
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.5 |
| Published | 2017-09-22 |
| First published | 2017-06-11 |
| Weekly downloads | 0 |
| License | GPL-3.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=7.0.0 |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Vladimir Shurygin |
| Maintainers | shuvava |
| Keywords | Http, testing, development |

## Links

- npm: https://www.npmjs.com/package/dev-http-server
- Repository: https://github.com/shuvava/dev-http-server
- Homepage: https://github.com/mrshuvava/simple-http-server
- Issues: https://github.com/shuvava/dev-http-server/issues
- npm.io page: https://npm.io/package/dev-http-server

## Dependencies (2)

- [mime](https://npm.io/package/mime.md) ~2.0.2
- [lodash](https://npm.io/package/lodash.md) ^4.7.0

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 0.0.5 (latest) — 2017-09-22
- 0.0.4 — 2017-08-07
- 0.0.3 — 2017-06-17
- 0.0.2 — 2017-06-17
- 0.0.1 — 2017-06-11

## README

# 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
```javascript
// 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
```javascript
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](https://github.com/shuvava/dev-http-server/tree/master/examples/rest-static-response)
```javascript
// 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](https://github.com/shuvava/dev-http-server/tree/master/examples/rest-dynamic-response)
```javascript
// 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](https://github.com/shuvava/dev-http-server/tree/master/examples/static-content)
```javascript
// 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 });
```

---
_Source: https://npm.io/package/dev-http-server · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
