1

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

BatMUD
  • 307
  • 1
  • 2
  • 10
  • Are you just wanting something like `response.write('Helloooo Wolrd!!');` – Peter Olson Jul 12 '13 at 01:24
  • Mmmm...I dont think so.. do you reckon thats the best practise, because above javascript is 3 lines because its just an example. So my javascripts will grow bigger and bigger. Mostly Jquery. Hope thats not the way to do it :) – BatMUD Jul 12 '13 at 01:27
  • But if thats the way Node does it.. I'll take it, Still I think there should be a better way. Just thinking :) – BatMUD Jul 12 '13 at 01:29
  • Typically in Node you don't deliver web content from a hard-coded string in the code--you are just using a simple example. Typically you will route content from files. If you are making a non-trivial web application you will want to have the markup and client side scripts in separate parts of the project, instead of including it all in the server-side script. – Peter Olson Jul 12 '13 at 01:35
  • Yes thats what I wanna know. as you can see index.js is not my node js server. Its a separate file sent to the client when the request come to the server. Or is that I have to use something like Express to do this? – BatMUD Jul 12 '13 at 01:46
  • 1
    Express is a nice framework that make things easier, but you don't have to use it. But whether you use a framework or develop it yourself, you need some way to route the requests of the client to be delivered whatever files you want to deliver. – Peter Olson Jul 12 '13 at 01:55
  • Agree, Question Edited.. Please have a look – BatMUD Jul 12 '13 at 02:27

2 Answers2

1

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?

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
  • Lets say what I want to is the second code block. Can I do that without Express?? – BatMUD Jul 12 '13 at 02:18
  • Question Edited.. Please have a look – BatMUD Jul 12 '13 at 02:26
  • 1
    @BatMUD: The edit doesn't tell me much more about what behavior you really want in this instance. But in response to your earlier comment: of course you can do that. Express itself is built on top of Node, and you can do yourself everything that Express does; if your needs are really simple, then you can forgo its power and unneeded features, and build it yourself. But just remember that now you will then have to handle a second request for your script file and pipe through the content of that script in response. This is what tools like Express do. So it's possible, but not trivial. – Scott Sauyet Jul 12 '13 at 11:12
0

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?

Community
  • 1
  • 1
0xcaff
  • 13,085
  • 5
  • 47
  • 55
  • I'm sorry thats not what I'm looking for, I did console.log as my first line of code on node js :). I'm looking for a way to write client side javascripts. Let me add some muscle to my javascript so that its not misleading... – BatMUD Jul 12 '13 at 01:36
  • Oh do you want to make a script which detects when it is running in the browser and runs browserjs code and in node it runs nodejs code? – 0xcaff Aug 29 '13 at 20:32