且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Node.js中从客户端调用服务器端功能(例如,html按钮onclick)?

更新时间:2022-10-25 17:49:49

下面是一个使用Express和HTML表单的例子。 / p>

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

上面的代码将启动一个Express实例,它是Node的一个Web应用程序框架。 bodyParser()模块用于解析请求主体,因此您可以读取发布数据。然后它将监听路线 / 中的 POST 请求。

 < form method =postaction =/> 
< input type =testname =field1>
< input type =testname =field2>
< input type =submit>
< / form>

如果您提交该表单,请在 req.body $>对于路线 / ,您将得到结果:

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

运行一个函数,只需将它放在 POST 处理程序中即可:

  var foo = function(){
//做某事
};

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

//发送响应不会暂停函数
foo();
});

如果您不想使用Express,那么您可以使用本地HTTP模块, d必须自己解析HTTP请求主体。

  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(){
//解析数据
foo();
});
}
})。listen(80);


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

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