httpserv
Dead simple, unbloated HTTP(s) request handler, perfect for the programmer with better things to do.
How do I use httpserv?
- Create an
http
orhttps
server using default Node.JS libs. - Import
httpserv
- Use
httpserv.serve
as the server request handler
const http = ; // Or you could use HTTPSconst httpserv = ; // or "./httpserv" if you downloaded the sourceconst server = http;server;
Where do I put my html files?
- Create a directory called "serve", wherever your main server file is
- That's where
Note: other file extentions are available
What about handling POST and GET requests and all that jazz?
- Sure. Here's one that handles any method of request:
httpserv;
- Here's one that handles only GET requests:
httpserv;
- Here's one that handles both GET and POST requests:
httpserv;
- New and super exciting in version 1.2: Match with regex!
Note: I am bad at regex
httpserv;
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
Screw relative paths, amirite?
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!
httpserv;
Run it from anywhere! Here I am one directory up.
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
Does it support HTTPS?
httpserv
doesn't care
If you have been paying attention, you will have noticed that httpserv
doesn't actually create
a server for you, it handles the requests an already existing server might get. Therefore, you
can create either an HTTP
or HTTPS
server, and httpserv
will hardly notice the difference.
Notes
- Any request omitting an
.html
extention will be treated as an HTML request, if the exact file does not exist - MIME types (
Content-Type
) are determined based on the extention from the request. There is a small database 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