How do I write a client side Java script to run on a Node JS server

I was trying to run a simple client side javascript code in my Node JS server. But failed. My index.js file looks like below

 response.writeHead(200, { 'Content-Type': 'text/html'});
 response.write('Helloooo Wolrd!!');
 response.end();

Now how do I put below ordinary javascript snippet in to above index.js page to run on Node JS(I want to know "Node way" of doing it AKA "Node Best Practise"),

<script type="text/javascript">
   var ns = 1;

   if (ns == 1){
       alert('Oh..Node what have you done?');
   }else{
       alert('Node is not bad after all');
   }
</script>

What I'm Really doing: What I 'm really building is building a web page that runs on Node Js which will capture Gyro values like Device Orientation and Device Motion through a javascript of my iPhone and send it back to the web server. I can simply do this in Apache stack. But its too havey and slow. Thats why I want Node JS

I'm afraid it's still not very clear what you're looking for. Do you want an HTTP response that looks something like the following?:

Content-Type: text/html

Helloooo Wolrd!!
<script type="text/javascript">
   var ns = 1;

   if (ns == 1){
       alert('Oh..Node what have you done?');
   }else{
       alert('Node is not bad after all');
   }
 </script>

If so, then you can either inline the script tag markup, as Peter Olson suggested, or look at streaming this into the response from a file.

If you want to do this:

Content-Type: text/html

Helloooo Wolrd!!
<script type="text/javascript" src="myScript.js"></script>

with the contents of your script in the file myScript.js, then you would probably be best served with a tool like Express.

If you are trying to get the equivalent of an alert to run on your server-side, then user2456263 has it right. There's simply no way to accomplish that. console.log is going to be your closest equivalent, and it's not particularly close.

If it's not one of these things you're trying to do, can you explain again?

From what I can understand you are looking for an alert function in node. There really isn't one. If you just want to print something to the console ( the ugly black box ) you can use console.log like this:

 console.log(”Oh..Node what have you done?")

If you would like to not change a line of code you could wrap console.log in an alert function like this:

var alert = console.log;

And it will work as is. If you are looking for Dom interactions you can try jsdom.

EDIT :

If you want a javascript file which is capable of being run on both the client side and the server side you would do something like this.

if (typeof(process) !== 'undefined' && typeof(process.stdout) !== 'undefined') {
      console.log('You are running Node.js');
} else {
      alert("Why use console.log() when you can use alert?!");
}

This question is already answered in:

How to check whether a script is running under node.js?