0

nodejs is built upon javascript, but some methods like alert(), writeln(),... etc are not working in nodejs.

var http = require('http');

http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});

response.end(''+alert('server running')+''); // alert() not working here.
}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/');

How can I use these methods in nodejs programs.

1
  • 3
    alert is part of the window object found in browsers. use console.log to log to the terminal Commented Feb 21, 2014 at 11:38

3 Answers 3

1

These are the browser functions that your trying to call out. You do not have the access to these global objects like window, document, as these are only browser specific.

The rewritten example would be:

var http = require('http');

http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});

  console.log('This will be written in your console');
  response.end('server running');  // The response output
}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/');
Sign up to request clarification or add additional context in comments.

Comments

1

Those are browser specific methods, of course they don't work in node.

Try console.log( whatYouNeedToLog ) instead.

Comments

1

You can't. They don't make any sense in the context of NodeJS.

If you want to run those functions in the browser, then send the browser an HTML document with embedded JS and not a plain text document.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.