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-->
If you need to call a NodeJS function (assuming it has to interact with the other server side code such as databases etc) then you can send a request, either via AJAX or standard HTTP, to the a route on the server and call that function within the route.
Node. js executes JavaScript code in its environment on the server, whereas Angular is a JavaScript framework that gets executed on the client (i.e. within a web browser.)
Node. js is a server-side, packaged software that contains predefined processes to accomplish specific tasks. As a server-side runtime, every Node. js process is executed on a server; essentially working on the backend aspect of an application to manage data.
JavaScript is a programming language, it can be run in a number of different environments. Most people run into it in browsers but it can also be used at the command-line via Rhino or currently on the server-side using Node. js Since it's inception back in 1996 JavaScript has been able to run on the server-side.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With