如何在 Node.js 中从客户端(例如 html 按钮 onclick)调用服务器端函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18831783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a server side function from client side (e.g. html button onclick) in Node.js?
提问by Saad Abdullah
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#.
我需要在 Node.js 中从(客户端)html 按钮 onclick 事件调用服务器端函数的完整基本示例,就像在 ASP.NET 和 C# 中一样。
I am new to Node.js and using the Express framework.
我是 Node.js 的新手并使用 Express 框架。
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-->
回答by hexacyanide
Here's an example using Express and a HTML form.
这是一个使用 Express 和 HTML 表单的示例。
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 /
.
上面的代码将启动一个 Express 实例,它是 Node.js 的 Web 应用程序框架。该bodyParser()
模块用于解析请求正文,因此您可以读取发布数据。然后它将侦听POST
路由上的请求/
。
<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:
如果您提交该表单,在req.body
route 中/
,您将得到结果:
{ field1: 'form contents', field2: 'second field contents' }
To run a function, just put it inside the POST
handler like this:
要运行一个函数,只需将它放在POST
处理程序中,如下所示:
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.
如果您不想使用 Express,那么您可以使用本机 HTTP 模块,但您必须自己解析 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() {
// parse the data
foo();
});
}
}).listen(80);