nullpointr nullpointr - 1 month ago 16
Jade Question

Pass form data from jade in Express.js

I've got the following small express.js app and the corresponding .jade file. Server is running without errors but when I submit the button, nothing happens. I would expect that the app.post(...) is called, but it is not.

Could someone point me to the mistake?



var express = require('express');
var http = require('http');
var bodyParser = require ('body-parser');

var app = express();


var port = process.env.PORT || 9250;
var host = process.env.HOST || "127.0.0.1";

var server = http.createServer(app).listen(
port,
host,
function() {
console.log("Server listening to %s:%d within %s environment",
host, port, app.get('env'));
});


app.set('views', __dirname + '/views');
app.engine('.html', require('jade').__express);

app.use( bodyParser.json() );
app.use(bodyParser.urlencoded({ extended: true }));


app.get('/', function(req, res) {
res.sendFile(app.get('views') + '/index.html')
});

app.get('/test', function(req, res) {

res.render('home.jade');
});

app.post('/claim', function(req, res) {
console.log('called');
res.redirect('/');
});

doctype html
html(lang="en")
head
title Hello

body
h1 Welcome
p Description

form(action="/claim", method="post")
label Input something
br
input(type="text", name="address", size=50)
br
input(type="submit", value="Submit")




Answer

Your problem is indentation : Jade doesn't understand your input is included in form.

Try this, it works for me :

doctype html
html(lang="en")
  head
    title Hello

body
  h1 Welcome
  p Description

  form(action="/claim", method="post")
    label Input something
    br
    input(type="text", name="address", size=50)
    br
    input(type="submit", value="Submit")