Node.js–Serving Up Web Pages with Dynamic Content
Building a prime number calculator - dynamic content
I’ve been fixing my algorithm below because a blog reader has given me advice. I like this.Getting things right is a great thing.
In this next section we will build a Node.js application that demonstrates the ability to send web content to clients. In this case, the Node.js application will calculate Prime Numbers and send the web content to an http client.
The code is very simple – you just build up the string of html content and output the string.
response.write('<html><body><p>Text or HTML sent to browser..</p></body></html>');
Here is some documentation for my code.
Code | Explanation |
---|---|
http = require('http'); | Because this is a web server |
request.addListener('end', function () { [ your code ] }); | An event triggered when there is an http request |
response.write('<html><body><p>' + buf + '</p></body></html>'); | Sends html back to the browser (based on original request) |
response.end(); | Concludes the HTML request |
listen(8000, 'localhost'); | Indicates to wait on Port 8000 of localhost |
The code
Here is the code listing for ‘pageCreate.js’.
http = require('http');
http.createServer(function (request, response) {
request.addListener('end', function () {
var buf = '<div style=\"color:green;\">' + '2' + ' is prime. </div>';
for (var i = 2; i < 500; i++)
{
for (var j = 2; j < i; j++)
{
if (i % j == 0)
{
var prime = 0;
var tmp = '<div style=\"color:red;\">' + i +
' is not prime. It is divisible by ' + j + '</div>';
buf += tmp;
break;
}
else
var prime = 1;
}
if (prime == 1)
{
var tmp = '<div style=\"color:green;\">' + i + ' is prime. </div>';
buf += tmp;
}
}
response.write('<html><body><p>' + buf + '</p></body></html>');
response.end();
});
}).listen(8000, 'localhost');
console.log('system waiting at https://localhost:8000');
The output
Here is what the browser displays as a result of the request, https://localhost:8000
Node.js sucks at CPU bound operations – it is about I/O
Node.js is terrible for CPU bound tasks because it is inherently single threaded. That means that if you are wanting to use multiple cores to perform some complex mathematical analysis, Node.js is not the best fit. Node.js has a single threaded event loop which takes only a single task/event at a time and processes it.
Where Node.js really shines is I/O bound tasks. Node.js allows you to simply define a callback that will get added to the event queue. Node.js will kick off a thread or process (some OS's don't do threads) for that I/O operation. And when that I/O operation is complete, the callback is invoked and the code continues. The great news here is that Node.js will process other I/O bound requests while others are still pending a return.
If you do have long running computations or similar needs, then you consider interleaving CPU intensive tasks with other events.
This post could get you started.
Faking threads | https://howtonode.org/understanding-process-next-tick |
Before I forget – the docs
Note below that Node.js does indeed have official documentation.
Node.js docs | https://nodejs.org/docs/v0.4.7/api/all.html#util |
Comments
Anonymous
February 27, 2012
Unfortunately, 0 and 1 are not prime numbers. cf : en.wikipedia.org/.../Prime_numberAnonymous
February 28, 2012
Cool that you edited the document, but 2 is actually a prime number. Your outer loop should start at i=2. Also, why testing "i % j == 0" twice instead of putting the code in a "else" statement ?