0

i have been trying to understand how JavaScript can function as server-side language as i am used to JavaScript for client-side work such as AJAX. can someone explain to me succinctly, i have Java and OOP experience and can't wrap my head around the fact that since JS is stateless.

Much thanks, if answers are really in-depth and profound i will make this into a community wiki. I know nodeJS accomplishes server-side coding using JS, but is it because it is compiled using Google V8 engine?

On the other hand, in AJAX, JS is used as logic on the page..

LaneLane
  • 345
  • 7
  • 16
  • 2
    Check out [node.js](http://nodejs.org/) – Eric Nov 20 '12 at 19:24
  • 1
    Wait - where did you hear that JS is stateless? – Matt Nov 20 '12 at 19:25
  • 1
    "JS is stateless" - shat does that even mean? The language allows mutable state just fine (in fact, you use it almost every time you use the `=` character, or one of numerous methods). You may be talking about the browser sandboxing the JavaScript code it runs (and in the process making persistence of state across page visits harder -- but even here, you have cookies, numerous new APIs like WebSQL and IndexedDB, and possibly more), but then you're confusing a language with an implementation of that language. –  Nov 20 '12 at 19:25
  • How can a language be stateless? A program must have some state – Ruan Mendes Nov 20 '12 at 19:25
  • V8 specifically has nothing to do with running programs on a server, or anywhere else, apart from being *one* potential way to do it (and being actually used for it). You seem to be confused about very basic concepts of program execution. –  Nov 20 '12 at 19:30
  • @LaneLane You may want to explain in more detail what has you confused. Because now we appear to be confused. – Alex Wayne Nov 20 '12 at 19:31

1 Answers1

3

What do you mean javascript is stateless? Here's a simple node.js server with transient state (lost on a server reboot):

var http = require('http');

var someState = 0;

http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Javascript has state: ' + someState++ + '\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

I know nodeJS accomplishes server-side coding using JS, but is it because it is compiled using Google V8 engine?

That's like asking "I know you can do server-side coding with PHP, but is it because it requires a PHP runtime?"

Eric
  • 95,302
  • 53
  • 242
  • 374