Lightnode is designed around the ideas of attaining a more ideal web server platform, which should be easier to manipulate and understand than our traditional servers, while still providing all the functionality that we desire for rapid development.
Node.js provides a solid base to use along our path towards a more ideal web server platform, however, as noted, it achieves this by purposely avoiding the provision of high level functionality in order to avoid the need to address the design challenge of countering the complexity and reduced flexibility inherent in introducing higher level functionality.
This means that it is up to library authors to tackle the challenge of providing a framework that provides the desired functionality while maintaining simplicity, and flexibility and ease of manipulation.
Lightnode addresses this challenge by conceptualizing server control in a manner inspired by our browser DOM, and with the most appealing conceptually coherency.
The user creates a hierarchy of server objects prior to when the server starts listening for requests. By creating a hierarchy of server objects, each object can be customized differently, and remains conceptually separate.
A simple delegation step is added to the server objects, where users can indicate how a request event is routed down the hierarchy. It is easy to route a request to some custom code outside of the server hierarchy.
A site can be served simply with just one server, or a separate server object can be created for each sub section of the site. Servers can also be created dynamically when a request comes in, including lazily created for the case where a non-existing server is requested from the hierarchy.
The servers in the hierarchy can be instances of the user's custom subclass of the lightnode.Server control class, or of it's file server subclass lightnode.FileServer.
Lightnode currently provides this simple hierarchical delegation framework for control, as well as add-on utility of fast static file serving with http client side caching, and a design that is easy to setup virtual hosting or any other desirable or customized setup. Much more functionality is planned to be added in future, in order to provide a platform for flexible and simple rapid development.
Although Lightnode is a fully free and open source product, we are collecting a small fee for it here on binpress in order to fund the development so that we can provide a platform that supports developers in creating better software more easily. You can choose to pay for the product (when you mouse over "download") or just download it free, if you'd rather not support the development at this point, or if you'd just like to test it out first.
Enjoy.
var mySite = new lightnode.FileServer("/home/web/mysite.com")
mySite.receiveRequest(request, response)
mySite.delegateRequest = function(req, resp) {
return someOtherServer
}
mySite.getChild('images')
mySite.constructChild = function(name) {
return new MyServerType()
}
// the server will automatically look for a child server for a request,
// you can control what the name it looks for the child server under.
mySite.getChildName = function(req) {
if (req.url.startsWith('images'))
return 'images'
else if (req.url.startsWith('posts'))
return 'posts'
}
// any child server that is found under the name will be delegated to,
// if it doesn't exist then no delegation is done and the server handles the request itself.
// so there is no need to do anything else except
// create the child servers before any requests come in
mySite.getChild('images')
mySite.getChild('posts')
var http = require('http')
var lightnode = require('../lightnode')
// 1 - create and start the node ip server
var server = new http.Server(); server.listen(8081)
// 2 - create the file server for the root directory
var website = new lightnode.FileServer('/home/web/www.ngspinners.com')
// when a request comes to the ip server
server.addListener('request', function(req, resp) {
// pass all requests to the file server
website.receiveRequest(req, resp)
})
// specify how the delegation is done before any requests come in
// the delegateRequest function should just return the
// lightnode.HttpServer object that must emit (handle) the request,
// or a function to execute as the request listener.
website.delegateRequest = function(req, resp) {
// 3 - requests starting with path '/api' are sent to the api server
if (req.url.indexOf('/api') == 0)
return apiHandler
// 4 - serve all other requests with this file server object
else
return website
}
// this is run when the given request must be handled by the API
function apiHandler(req, resp) {
// I'm a hello world API
var message = 'hello, world.'
resp.writeHead(200, {
'content-type': 'text/plain',
'content-length': message.length
})
resp.write(message)
resp.end()
}
excellent !..
This is a great component, well written and documented, could be better set out but over all a very good library.
Questions & Comments