Serverside and clientside javascript

Does serverside javascript exist, if yes, is it possible to clientside javascript to interact with serverside javascript?

For example, is it possible for clientside javascript to request from the serverside javascript to return part of a json file which is stored on the server without downloading the whole json file?

I understand that I can do this with other serverside languages like aspx, php etc etc, but wanted to know if serverside javascript exists and if it can do something similar with json files?

Problem courtesy of: oshirowanen

Solution

Does serverside javascript exist

http://en.wikipedia.org/wiki/Comparison_of_server-side_JavaScript_solutions

Node.js is the current, popular way to do this.

if yes, is it possible to clientside javascript to interact with serverside javascript?

Yes. In the context of the WWW, this is usually done the same way that any other communication with server side programs is done: By making HTTP requests (usually via XMLHttpRequest).

For example, is it possible for clientside javascript to request from the serverside javascript to return part of a json file which is stored on the server without downloading the whole json file?
I understand that I can do this with other serverside languages like aspx, php etc etc, but wanted to know if serverside javascript exists and if it can do something similar with json files?

JavaScript is a programming language. It can do more or less anything on the server that any other language can do.

Solution courtesy of: Quentin

Discussion

Backend and frontend technologies almost always communicate via HTTP request/responses in general. This allows any pair of technologies to communicate in a language agnostic manner.

Discussion courtesy of: thatidiotguy

Client side with jquery:

$(document).ready(function(){
  function fireAjax(){
    $.getJSON('/ajax/partialJSON', {desiredKey:'foo'}, function(data){
      console.log(data);
    });
  };
  $('#button').on('click', fireAjax)
};

Server side, using node.js and the Express web framework:

app.get('/ajax/partialJSON', function(req,res){
  var key = req.query['desiredKey'];
  // parse your JSON here
  var data = {foo:'Hello;', bar:'World'};
  var value = data[key] || null;
  res.json(value);
});

Discussion courtesy of: Plato

Going on what Quentin said above

Server side JavaScript is completely possible yes, using Node.js will also offer you a much wider range than simple XMLHttpRequest's.

Using modules with NodeJs try Websocket.io allows you to talk between Client and Server very much more easily.

Discussion courtesy of: Nick White

This recipe can be found in it's original form on Stack Over Flow .