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-->
1 个解决方案
#1
27
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的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中,您将获得结果:
{ 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);
#1
27
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的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中,您将获得结果:
{ 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);