Project 5: Tweet faker in node.js

by samscaife on Wednesday 2 May 2012

I wanted to get my Tweet faker working in node before I tried pulling in live information. Node.js has the ability to install little programs and libraries into your code which allow you to easily do a whole host of different things. One of these is called File System, or fs. With this you can do a load of different functions with your files what I was interested in though was the ability to alter the section of my server which controlled what to deliver to the user so that It would give a file instead of text.

I managed to figure out how to do this by changing the string which was delivered to a variable which was loaded with the file information from fs, here is an example of the code;

fs.readFile('./index.html', function (error, data) {
if (error) {
throw error;
}
index = data;
});

function start(response) {
console.log("Request handler 'start' was called.");

response.writeHead(200, {"Content-Type": "text/html"});
response.write(index);
response.end();
}

This is just the small section which delivers the html file to the rest of the code. Using this method I was able to get my sever delivering the html and js files so I had my fake tweet visualisation running. This method however runs multiple versions of my twitter api key so if I where to go live with this I would get banned. To stop this I need to migrate my code to the server then use JSON to request the variables

Leave your comment