简体   繁体   中英

Express.js and form validation

I'm tring to create a simple node- with express.js script that will sum 3 numbers.

On index I have this:

index.jade

!!! 5
html
  head
    title Test
  body
    form(name='form1', method='post', action='/')
      label(for='1')
      input#1(type='text', name='1')
      label(for='2')
      input#2(type='text', name='2')
      label(for='3')
      input#3(type='text', name='3')
      input(name='submit', type='button', value='submit')
    #result

and also i'm now writing the serverside - app.js with req and res object but how to return a result... also result = 1id + 2id + 3id

app.js

var express = require('express');
    app = express.createServer();
app.use(express.bodyParser());
app.post('/', function(req, res){
  var i = req.param('1', null);
  var j = req.param('2', null);
  var k = req.param('3', null);
  var r = i+j+k;
  res.send(r);

});

How I to send results (r) into div id Result on index.jade... so how to return result to index.jade

also here is pastebin code: http://pastebin.com/J9MRFCaE ... i'm new to node and express and sorry for stupid question...

It's simple, just call your "index.jade" rendering passing your data (instead of 'res.send(r);') :

res.render('index', { 
  result: r
});

And display "result" variable in your jade file :

#result #{result}

Additional information on jade code and express rendering

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM