0

I'm running a nodejs server based using the example here:

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser());
app.get('/', function(req, res){
  var html = '<form action="/" method="post">' +
               'Enter your name:' +
               '<input type="text" name="number1" >' +
               '<br>' +
               '<input type="text" name="number2" >' +
               '<br>' +
               '<button type="submit">Submit</button>' +
            '</form>';

  res.send(html);
});

app.post('/', function(req, res){
  var number1 = req.body.number1;
  var number2 = req.body.number2;
  var result = eval(number1+'+'+number2)
  var html = result + '.<br>' +
             '<a href="/">Try again.</a>';
  res.send(html);
});

app.listen(80);

Is there any way that for example if I perform some calculations inside the app.post function they run on the client side and not on the server side?

David
  • 929
  • 1
  • 11
  • 17
  • No, you’d have to write some client-side JavaScript. Node.js doesn’t really change anything about that model. – Ry- May 19 '15 at 21:54
  • Your node server runs on your machine because NodeJs is installed on it. – Kevin Boucher May 19 '15 at 21:57
  • can't I send a nodejs file to run on the client browser console and send me back results? – David May 19 '15 at 21:57
  • It can’t use Node.js features at that point. You can send environment-agnostic code, if you like – I don’t think it’s worth the hassle, but some frameworks try to make it work. – Ry- May 19 '15 at 21:58
  • Is https://stackoverflow.com/questions/4371196/node-js-and-client-sharing-the-same-scripts what you want to do? – Ry- May 19 '15 at 22:00

0 Answers0