33

I need a complete basic example in Node.js of calling a server-side function from (client side) html button onclick event, just like in ASP.NET and C#.

I am new to Node.js and using the Express framework.

Any help?

IMPROVED QUESTION:

//server side :

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var app = express();

// all environments

app.set('views',__dirname + '/views');
app.set('port', process.env.PORT || 3000);
app.engine('html', require('ejs').renderFile);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
app.set('view engine', 'html');

app.use(app.router);

app.get("/",function(req,res)
{
  res.render('home.html');
});


// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

//Client Side

 <input type="button" onclick="" />  <--just want to call the serverside function from here-->
Flip
  • 6,233
  • 7
  • 46
  • 75
Saad Abdullah
  • 2,252
  • 3
  • 29
  • 42
  • 4
    You need to communicate with the server over HTTP, using AJAX or a form POST or websockets. – SLaks Sep 16 '13 at 15:32
  • I have googled alot, but unable to get the correct example, – Saad Abdullah Sep 16 '13 at 15:40
  • @SaadAbdullah: What don't you understand? Node.js is a much thinner web platform; you need to understand the underlying technologies (HTML and HTTP). – SLaks Sep 16 '13 at 15:42
  • @SLaks: thanx for your response sir, I am making replica of wikipedia for my web-programming semester project, (means, I am a noob with node.js) I am stuck at some point, I just want to know that how can i use the server side function from client side event? – Saad Abdullah Sep 16 '13 at 15:54
  • 1
    @SaadAbdullah: You can't call a function on one computer from another computer. You need to send a request over the network. – SLaks Sep 16 '13 at 19:49
  • As I said earlier, you _need_ to understand the underlying protocols. You can try to use higher socket abstractions, but you will fail horribly if you don't understand what's going on. – SLaks Sep 16 '13 at 19:49

1 Answers1

47

Here's an example using Express and a HTML form.

var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);

app.use(express.bodyParser());
app.post('/', function(req, res) {
  console.log(req.body);
  res.send(200);
});

server.listen(process.env.PORT, process.env.IP);

The code above will start an instance of Express, which is a web application framework for Node. The bodyParser() module is used for parsing the request body, so you can read post data. It will then listen for POST requests on the route /.

<form method="post" action="/">
  <input type="test" name="field1">
  <input type="test" name="field2">
  <input type="submit">
</form>

And if you submit that form, in req.body for the route /, you will get the result:

{ field1: 'form contents', field2: 'second field contents' }

To run a function, just put it inside the POST handler like this:

var foo = function() {
  // do something
};

app.post('/', function(req, res) {
  console.log(req.body);
  res.send(200);

  // sending a response does not pause the function
  foo();
});

If you don't want to use Express then you can use the native HTTP module, but you'd have to parse the HTTP request body yourself.

var http = require('http');
http.createServer(function(request, response) {
  if (request.method === 'POST') {
    var data = '';

    request.on('data', function(chunk) {
      data += chunk;
    });

    request.on('end', function() {
      // parse the data
      foo();
    });
  }
}).listen(80);
David Rust-Smith
  • 473
  • 4
  • 11
hexacyanide
  • 88,222
  • 31
  • 159
  • 162