Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

javascript - How to call a server side function from client side (e.g. html button onclick) in Node.js?

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-->
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...