Tag Archives: clients

I love NodeJS

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.

NodeJS the Serverside JavaScript Framework

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.

Node running our Server.js file

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.

A 404 generated by node when it can't find the file we requested

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.

Tagged , , , , , , ,

What I do, is not What I do.

It’s an odd truism in the website world but it’s often the case. What I do, is not What I do.

How many of us introduce ourselves like this: “Hi, I’m Dave, and I design websites”? Seriously, I know I do, but that isn’t even half the story given the myriad of different skillsets that roll into those 2 words. “I design websites”. What the fuck does that even mean when you get down to it?

After all my skill sets boil to so much more than just those 2 words. But we all simplify for the client. After all saying “I’m a website designer” is easier than saying “I’m a website designer and developer with a specialization in PHP and using WordPress as a Content Management System as well as being versed in various aspects of SEO for businesses and building Social Media Presence”.

But we kind of have a tendency to shoot ourselves in the foot by saying Website Designer, as so many people (mostly the non tech-literate) start making assumptions about what you can do.

I recall one instance where I told someone I was a website designer and I was requested to build an all singing and dancing flash animation for their website. I had to decline, after all I’m not an Animator. But the thing that stuck with me was the parting words. “I thought you said you were a web designer”.

It’s a huge misconception among the average lay-person as to what web design actually is.

So to my “Web Designer” friends, have a read of this and if you have any funny stories to tell do so. But I would like to hear how you describe yourself to new clients.

Tagged , , , , , , , , ,

Spec Work Kills Industries

As a designer, I hate Spec Work. It’s so frustrating and it irks me that there are so many people still thinking it’s a good way of getting jobs. It’s really not. Spec work kills good design and opens up so many problems. And now I’ll tell you why.

Spec work stands for Speculative Work (Sometimes called Crowd Sourcing), where by a client will ask you to produce a design or a piece of work and if they like it they will buy it.

This is not cool. If I spend hours working on a design, I expect to be paid for those hours, not for the promise that I ‘might’ be paid if they like the work. Plus in the Spec Work environment you are usually “Competing” against potentially hundreds of other designers at any one time for a job in hand. Which essentially wastes my time, if I’m not awarded the contract. I’d much rather do the work for a straight up paying client off the top.

But the thing that irritates me the most is the fact that people think that they can push it with web designers and developers. You wouldn’t walk into a tattoo parlor and ask the Tattooist to make you a design and if you like it you might get it tattoo’ed. I guarantee that if you walk into any tattoo parlor and try that then you will likely be chucked out faster than you can say “Wait I was joking”.

Where as, as a web designer, I will get approached with concept ideas that people want me to do, with the line “But if we don’t like it, at least it’ll look good in your portfolio”. Well much like any Tattoo shop, every item in my portfolio is a piece I have either produced for myself or for a paying customer. You can’t play that card with me.

About the only people whom I would ever recommend do Spec work, are students with little to no portfolio as yes, they can get some designs out in the wild and they might get noticed, but once you start getting real clients that approach you with a budget, drop the spec work. Those clients are the ones you want. Mostly because if you do a good job they will give you repeat business.

This is a pretty hot topic in the design world, and it always will be, but I make my stance, spec work is evil. Don’t do it.

Dave the kill of Spec Work.

Tagged , , , , , , , ,

Stressed? Chill Out!

So looking at today’s Daily Post topic, Stress is on the menu it would seem. I foresee hundreds of posts about people posting about how stressed their individual lives are, and how some of the minutiae has finally caught up with them and cracked their sanity enough to feel bad about things.

Why? Why do people let themselves feel stressed? It certainly isn’t conducive to productivity or work in any way shape or form. Generally speaking it simply makes your hearts work faster and drives you towards an earlier death.

I’ve often thought that people need to get a better grip on their stressful lives and simply cut out some of the crap that makes them stressed on a day to day basis. I’m almost positive that if you thought hard about the stuff that was making you feel stressed you’d find some way to get it away from you.

I have every excuse to be stress. University work, Starting a Business, Clients being pains, Rent, Family, money, a distinct lack of coffee in the house that will ultimately drive me to murder because I require sweet, sweet nourishing caffeine.

I could get stressed about these things, but at the end of the day, I choose not to, simply because there is no point. I can do as much to change the way these things work as I can to change the weather patterns of Space. So I don’t stress about them. I simply roll with the punches. Meditation helps me in this sense as it gives me a clear and objective free space in my mind which I can use to focus.

Perhaps it can be attributed to our constant information demand culture. Everything updating at once everywhere, and you start feeling out of touch with everything that’s going on.

Either way. Chill out, Grab a cup of Tea. De-stress. And most importantly have some fun! Hit me up for a chat and I’ll try to amuse you.

Or tell me how you manage your stress. I meditate, what do you do?

 

PS. I wasn’t joking, deliver the coffee or I swear I shall murder some people for their coffee supplies.

Tagged , , , , , , , , , , ,