2.0.3 • Published 6 years ago

sans-server v2.0.3

Weekly downloads
128
License
Apache-2.0
Repository
github
Last release
6 years ago

npm module downloads Build Status Coverage Status

#sans-server

Write code for a server, without the server.

  • Make requests that are representative of HTTP requests to a function.
  • Get responses that are representative of HTTP responses.
  • Accepts connect middleware.
  • Easier to test your server code (using direct function calls).
  • Faster to test your server code (no HTTP required).
  • Can be wrapped by any server.
  • Easy to read logging.

Example

The sans-server package is a tool for building a functional web server that is independent of networking. As a functional library, requests can be made to it and responses provided by it.

const SansServer = require('sans-server');

// create a server instance
const server = SansServer();

// add middleware to the server
server.use(function(req, res, next) {
    if (req.path === '/') {
        res.send('OK');
    } else {
        next();
    }
});

// make a request against the server
server.request({ path: '/' }, function(response) {
    console.log(response.statusCode);   // 200
    console.log(response.body);         // 'OK'
});

// make a request against the server
server.request({ path: '/foo' }, function(response) {
    console.log(response.body);         // 'Not Found'
    console.log(response.statusCode);   // 404
});

// make a request that returns a promise
server.request({ path: '/' })
    .then(function(response) {
        console.log(response.statusCode);   // 200
        console.log(response.body);         // 'OK'
    });

Routing

Routing is not built into sans-server so you'll want to use some routing middleware.

Documentation

  • Sans-Server Instance - An instance can be generated with a configuration. Once an instance exists it's easy to specify middleware and to make requests.

  • Middleware - The server does almost nothing as is. You need middleware to add functionality.

  • Request Object - This object is passed into every middleware function and has information about the request made.

  • Response Object - This object is passed into every middleware function and is used to produce the response.

1.5.2

6 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.5.1

7 years ago

1.5.0

7 years ago

1.4.3

7 years ago

1.4.2

7 years ago

1.4.1

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.0.32

7 years ago

0.0.31

7 years ago

0.0.29

7 years ago

0.0.28

7 years ago

0.0.27

7 years ago

0.0.26

7 years ago

0.0.25

7 years ago

0.0.23

7 years ago

0.0.22

7 years ago

0.0.21

7 years ago

0.0.19

7 years ago

0.0.18

7 years ago

0.0.17

7 years ago

0.0.16

7 years ago

0.0.15

7 years ago

0.0.14

7 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago