Dead simple, unbloated HTTP(s) request handler, perfect for the programmer with better things to do.
npm install algo-httpserv- Create an http or https server using default Node.JS libs.
- Import httpserv
- Use httpserv.serve as the server request handler
``js`
const http = require("http"); // Or you could use HTTPS
const httpserv = require("httpserv"); // or "./httpserv" if you downloaded the source
const server = http.createServer(httpserv.serve);
server.listen(serverOptions.port);
- Create a directory called "serve", wherever your main server file is
- That's where
Note: other file extentions are available
- Sure. Here's one that handles any method of request:
`js`
httpserv.on("/api_endpoint/", (request, response, url) => {
response.writeHead(200, {"Content-Type": "application/json"});
response.write(JSON.stringify(url.query));
response.end();
});
- Here's one that handles only GET requests:
`js`
httpserv.on("/api_endpoint/", "GET", (request, response, url) => {
response.writeHead(200, {"Content-Type": "application/json"});
response.write(JSON.stringify(url.query));
response.end();
});
- Here's one that handles both GET and POST requests:
`js`
httpserv.on("/api_endpoint/", ["GET", "POST"], (request, response, url) => {
response.writeHead(200, {"Content-Type": "application/json"});
response.write(JSON.stringify(url.query));
response.end();
});
- New and super exciting in version 1.2: Match with regex!
Note: I am bad at regex
`js`
httpserv.on(/\/regex-endpoint\/*/, ["GET", "POST"], (request, response, url) => {
response.writeHead(200, {"Content-Type": "application/json"});
response.write("Regex requirement met!");
response.end();
});
*For the curious minds, the third parameter ("url") is just the result of url.parse(request.url, true),
which is convenient to have when handling your own requests. "request" and "response" are directly
from the server*
New in version 1.2: kick them to the curb!
use the function setServePath and the variable __dirname to make your script run from anywhere on the drive.
Take a look!
`js`
httpserv.setServePath(__dirname + "/serve");
Run it from anywhere! Here I am one directory up.
!Relative paths are for losers
Or don't do that!
- If that's too much work for you, httpserv still runs off relative paths by default. Which is ok enough I guess
- httpserv doesn't care
If you have been paying attention, you will have noticed that httpserv doesn't actually createHTTP
a server for you, it handles the requests an already existing server might get. Therefore, you
can create either an or HTTPS server, and httpserv will hardly notice the difference.
- Any request omitting an .html extention will be treated as an HTML request, if the exact file does not existContent-Type`) are determined based on the extention from the request. There is a small database
- MIME types (
included in the library with common items. If you need to modify them, just do it in the source code.
They're right near the top. Of course, this behavior is ignored with custom handlers.
- File serving is stream-based
- Files do not cache (Although, I have interest in implementing this)
- No dependencies
- Streaming audio and video should be okay, but it is untested