An attempt to make an emacs lisp clone of nodejs.
Apparently Steve Yegge has already done this... but I couldn't find it so I'm writing it.
It's like "hell, no". It's... well.... it's an, um, joke.
How does it work?
Well... you can define a handler function:
(defun nicferrier-handler (httpcon) "Demonstration function" (elnode-http-start httpcon "200" '("Content-type" . "text/html")) (elnode-http-return httpcon "<html><b>HELLO!</b></html>") )
And then start the server:
(elnode-start 'nicferrier-handler 8000)
You can also start the server interactively... you still have to pass it a handler function and a port.
Stopping the server
If you can remember the port you started your server on then you'll be able to stop it, like:
You can query headers in the HTTP request in your handler using the function elnode-http-header:
(defun my-handler (httpcon) (elnode-http-header httpcon "Content-type") )
this returns an alist of the params in the query.
Handler functions can be simple functions to respond to everything coming into a server. It's more normal that you would have a server that did different things on different urls so elnode provides a url dispatcher function, you can use it like this:
(elnode-dispatcher httpcon '(("/$" . 'root-handler) ("users/$" . 'users-handler))))
One of the chief advantages of any lisp is the amount of flexibility there can be in code like this. If elnode is at all successfull at catching people's imaginations I would expect a variety of dispatchers to emerge.
There is early support for calling unix processes asynchronously:
(elnode-child-process httpcon "cat" "/home/nferrier/elnode/example.html" )
Would call the program cat with the HTML file as an argument. The output of the process will be copied to the HTTP connection asychronously (so fast).
Again, lot's of improvement needed here but this does work quite well.
There's always a but.
The HTTP parsing isn't very complete, we don't parse any data sent through POST.
That stuff is coming soon.