Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Prime Number Test in node.js and express
//
// Created by John F. Mercer on Monday, December 5, 2011
// john.f.mercer@gmail.com
//
// tested on node 0.6.4 and express 2.5.1
//
var express = require('express'),
app = express.createServer();
app.use(express.logger());
var isPrimeNumber = function( number ) {
if (number == 1 || number == 2) {
return true;
}
for (var i=2;i<number;i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
app.get('/', function(req, res){
res.send('Hello, Optimus Prime.');
});
app.get('/is_prime_number',
function(req, res){
var number = parseInt( req.param( 'number' ), 10 );
if (isNaN(number) || number <= 0) {
res.send(number + " is not a valid number! Try again!");
return;
}
if ( isPrimeNumber( number ) ) {
return res.send( number + ' is prime!' );
} else {
return res.send( number + " is not prime." );
}
} // end function
);
app.listen(3000);
console.log('Express server started on port %s', app.address().port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment