-3

Getting started with Javascript. Needed an editor and found warm references on SublimeText, so I installed it. Good tool.
Since I wanted to test/run some code before putting it up on my server, I needed something to test code locally. Again returning to the internet, I found Nodejs and installed it. So far so good.
First test:

    alert("Hello World"); 

works fine on my website, doesn't work on Nodejs. I later found I need to use

    console.log("Hello World");

on Nodejs.. If it was just 1 difference, I could probably cope, but my next attempt

    var answer = prompt("just type in something");

failed too, for the same reasons..

I get that Nodejs and webservers are different beasts, and serve different purposes, but on the other hand: how do you test your code before putting it on your server?

AAAAAAHHHHH!!! This is driving me bonkers. Not to mention SublimeText accepts both (so that is not helping me either)

Is there a way to get Nodejs to accept the same code as would run on my server?

  • 1
    are you using node.js as server application or frontend? – NoOorZ24 Aug 03 '17 at 11:08
  • Looks like, you should start learning the difference between a web application and server application and then basics of javaScript (maybe take some courses in Khan Academy.. they're too good) and then start with Node.js. `alert` and `prompt` are browser specific methods. thats why they didn't work in Node's REPL. – Supradeep Aug 03 '17 at 11:18
  • one is executed in the browser, the other on the server? yep. noob material here. Khan: hard to find exact what you need. Some courses bore me in 10 secs, others are way over my head. – YoWithaBanjo Aug 03 '17 at 12:38

2 Answers2

0

I think you're mixing up webserver and browser.

NodeJs is a Javascript Runtime which executes on a webserver. Therefor any command that is tied to a browser runtime environment (like alert) won't work there.

If a user visits your site which executes Javascript it's executed in the browser runtime environment. Therefore commands like alert are working, but other maybe don't (like fs.readFile).

cyr_x
  • 13,987
  • 2
  • 32
  • 46
  • webserver vs in browser: interesting. Is there a way to make code either execute in the browser or on the server? I would like to have my server open up a file (which I as admin upload) and put the contents in a SQL database, on the other hand, I would like users to login and review the data belonging to their account. I would presume one is executed on the server, the other requires a cooperation between browser/server.. – YoWithaBanjo Aug 04 '17 at 07:41
0

If you look at alert and prompt on MDN (window.alert and window.prompt).

You can see those methods belong to the window object.

window is a DOM interface, see

https://developer.mozilla.org/en/docs/Web/API/Window

The problem you are encountering is because NodeJS is a server side application, it therefore means window is undefined, because NodeJS has no DOM. Calling window in NodeJS will result in the error "window is not defined".

You would call window on the client, not the server.

Further Reading:

Why doesn't node.js have a native DOM?

To answer your question, there are node packages which will give NodeJS a DOM, such as jsdom.

However, you should consider whether it is appropriate that you should use this or not, as I suspect you are simply just calling the wrong methods and you are trying to write client executed code on a server application, and you should preserve client/server separation. (In my experience, I have only ever given a server side application a DOM for bad integration tests).

Your webserver should respond to a request from a client, and have your client executed code handle it.

Jason
  • 3
  • 2
  • Ok, understand your point. But what is the best way of testing code? Don't want to dump things on the server just to test.. – YoWithaBanjo Aug 03 '17 at 12:42
  • Depends, sounds like you are trying to test client side code on a server app, so consider that first. Otherwise simply use console.log() or add jsdom to your project if it is client side code you are testing in nodejs. – Jason Aug 03 '17 at 16:41