sans-server v2.0.3
#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.
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago