Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Express.js and form validation

Express.js and form validation

Problem

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...

Problem courtesy of: Michael Froter

Solution

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

Solution courtesy of: Arnaud Rinquin

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Express.js and form validation

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×