DCT4SUQGCH2N
So I’ve not posted anything in a few weeks, mostly due to being busy and the like, so today as a little present I shall instead share some nice Logo concepts I have been working on with you all.
Enjoy
Dave, The Logo Artisan
So NodeJS has become all the rage in the development world recently, especially amongst those developers that use a lot of JavaScript and it certainly presents some interesting opportunities for those that know what they are doing with it.
Even more so if you couple it with other solid JavaScript libraries like SpineJS.
Essentially speaking for those of you that don’t know, JavaScript is usually a client side implementation in the browser to add extra functionality or small bits of programming logic. Think things like Jquery. In browser, adds functionality, is used as a solid library for manipulating elements and data on a page, and can be used to provide some very nice client-side validation.
NodeJS is on a whole other level.
Node is server side JS and is used to write Java Applications and acts as a solid framework for building applications on. Think python.
And today I intend to give you a little demonstration of just what you can do. In 37 lines I am going to create a web server that will function and serve static files (no fancy logic in this one). You can think of it as a portable web server on the go.
var http = require("http"), url = require("url"), path = require("path"), fs = require("fs") port = process.argv[2] || 8888;
So first we initialise some variables. These are Node includes for the most part dictating which sub libraries of Node we want to include in our application. HTTP is used to handle HTTP requests. URL manipulates URL data, PATH allows us to manipulate local data paths (needed for finding files) and FS is included to read out files and serve them back out.
Now the beauty of Node is it’s designed to be non-blocking, and therefore can handle multiple asynchronous requests in one go. And it is with that in mind that the application makes a lot of use of callbacks, where the functions listen for an activity and wait to respond to it.
http.createServer(function(request, response) {}.listen(parseInt(port, 10));
BAM. That line has just made a http listener, that will listen for http requests coming in on the port we specified in the Variables above (8888). At the moment though it will literally do nothing except listen to a request.
We also want a line following that to actually provide a start to the application and give some feedback on the server. So we add:
console.log("Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown");
That will write to the console, letting us know that the server has actually started, and give us some details like the port we have specified.
Ok time to get some application logic in there.
http.createServer(function(request, response) { var uri = url.parse(request.url).pathname , filename = path.join(process.cwd(),"public/", uri); }).listen(parseInt(port, 10));
What we have now is listener that will look for incoming HTTP requests on port 8888 on the server and will attempt to parse the URL that is provided removing the host information to get a path string to a file, which it will append to the node instances current working directory.
So if my Node app is running on d:/node and I send a request to local host that is http://localhost:8888/files/ it will amend the path above to be d:/node/files/. Pretty nifty.
So now for a bit of validation. We amend the http.createServer function to the following.
http.createServer(function(request, response) { var uri = url.parse(request.url).pathname , filename = path.join(process.cwd(),"public/", uri); path.exists(filename, function(exists) { if(!exists) { response.writeHead(404, {"Content-Type": "text/plain"}); response.write("404 Not Found\n"); response.end(); return; } }); }).listen(parseInt(port, 10));
Excellent. Now we have some validation in there. path.exists checks the path variable we assign to filename to see if it exists. If it doesn’t it ends the request and responds with a 404 error. The response.writeHead function tells the page that made the request the type of content to expect. Then we write to the page our 404 message, finishing by ending the response.
Next we want to start serving some files back to users.
Amend the createServer function as follows:
http.createServer(function(request, response) { var uri = url.parse(request.url).pathname , filename = path.join(process.cwd(),"public/", uri); path.exists(filename, function(exists) { if(!exists) { response.writeHead(404, {"Content-Type": "text/plain"}); response.write("404 Not Found\n"); response.end(); return; } if (fs.statSync(filename).isDirectory()) filename += '/index.html'; }); }).listen(parseInt(port, 10));
Ok. Standard HTML time. We need to check to see if we are looking at a directory or at a file in the URL request.
http://localhost:8888/files/ is looking at a directory, so we would want to find the index of that directory. The line we just added uses the file system (fs) library we loaded to check if it is a directory. In this case it is, so it appends the path with a file name. Index.html. The standard root for all folders. http://localhost:8888/files/fred.html would not be a directory so it would leave the string as is.
Now we have a file that we are looking for we need to serve it back to the user at the other end. Amend the server function to this:
http.createServer(function(request, response) { var uri = url.parse(request.url).pathname , filename = path.join(process.cwd(),"public/", uri); path.exists(filename, function(exists) { if(!exists) { response.writeHead(404, {"Content-Type": "text/plain"}); response.write("404 Not Found\n"); response.end(); return; } if (fs.statSync(filename).isDirectory()) filename += '/index.html'; fs.readFile(filename, "binary", function(err, file) { if(err) { response.writeHead(500, {"Content-Type": "text/plain"}); response.write(err + "\n"); response.end(); return; } response.writeHead(200); response.write(file, "binary"); response.end(); }); }); }).listen(parseInt(port, 10));
Ok that was a nice chunk. So what this section is now doing is reading the file given by our filename string using the type binary.
It checks to see if the file can be read (if(err)). If the statement produces an error it sends a 500 server error response back to the user on the other end. If it doesn’t produce an error it sends the file back as binary, resulting in one web page being displayed.
One successfully loaded webpage. You'll note that the HTTP requests for CSS files and images have also been successful and returned as well
BAM. In 37 lines of JavaScript we have just created a working web server.
You can further extend this by adding other libraries like SpineJS to make fully JS MVC applications that will return more dynamic content (using JSON and then parsing it with AJAX requests).
It’s amazing what you can do with some languages, and that is why I love the NodeJS framework.
Go get it and go play!
Dave the Keeper of Nodes.
So it turns out that our governments are duplicitous creatures and apparently entirely in the deep pockets of the entertainment industry. Today I am thoroughly disgusted with the UK, which has agreed to ratify Anti-Counterfeiting Trade Agreement (ACTA). Fortunately we still have some time, as the issue still needs to be voted in the European parliament, and if it fails there, it fails globally.
Now that really irks me about ACTA is that it isn’t being passed as law, it’s being pushed through as a trade agreement, something that doesn’t require governments to have public approval. This means they have been able to push this through stealthily, and without making it a transparent agreement for the world to look at. Probably because if they had, the world would have gone into even more massive uproar than over SOPA.
This tactic of pushing through bills that affect laws and criminal codes is called Policy Laundering. And it is massively in breach of what people would perceive as our democratic rights, especially since it modifies the criminal code. This is a civil matter, and needs to be decided in parliament, with full exposure to the public, not made secretly hidden from the public.
After all, SOPA was merely an extension of the US Law, ACTA on the other hand is Global. There is no protection and no place to hide.
Given that the countries pushing it as well are supposed to be democratic nations, the fact that this has not been a democratic process in the slightest is a massive breach of the trust we place in our governments. It is about time that we made out governments stand up and listen to us as a nation. We have been giving over this power to them and now they are abusing it.
In a million ways.
For starters it basically requires that ISPs screen all content that passes along their networks for copyright material. To put it bluntly and really hit home. It basically involves them reading your emails.
Now I can guarantee that if the government demanded that your postal mail was all read before being sent out by you, you’d be pretty pissed. This is a huge breach of the fundamental rights and freedoms of us as European citizens. It completely restricts our freedom to expression and communications privacy.
For instance, post anything marked as copyright that you don’t realise and post to Facebook, means you lose your internet connection. No bullshit. ISPs would be required to disconnect you from the network. End of story.
Now tell me that isn’t a huge breach of my freedom to communications privacy, and my right to communication. Plus given that there was talk of access to information on the internet being classified as a basic human right, then this is now in breach of my human rights. HURRAY.
The Free Software Foundation (FSF) has published “Speak out against ACTA”, stating that the ACTA threatens free software by creating a culture “in which the freedom that is required to produce free software is seen as dangerous and threatening rather than creative, innovative, and exciting.
That quote from the Free Software foundation highlights a bit of the threat towards the creative industries as well. This bill would also mean the death of software repositories like GitHub and my personal favourite, Sourceforge as well.
Also to my fellow Unix and Linux brothers! You like music as well right. Well say goodbye to being able to play your music Collection. ACTA would make it illegal to play Non-Free media in open source media players.
That’s right; anyone who uses Open Source software to play DRM material will essentially be committing a crime. Tell me that is not completely fucked up in every single way.
UK based, then grab your MP and make some noise. Some serious noise.
And if you are European, then grab your MEP, same for us UK folks as well. Make noise. We need to kick off about this in a big way, not simply because it impinges upon our civil freedoms and our rights to communications privacy, but because it’s in breach of democratic process of the free nations of the world.
There is information all over the web to help point you in the right direction.
My suggested starting point is right at this website here: http://www.stopacta.info/
Stand up and fight.
Dave, the willing net warrior