node.js - Unable to post form details using Express js -
my express code:
var express = require('express'); var bodyparser= require("body-parser"); var app = express(); app.use(bodyparser.urlencoded({ extended: false })); app.set('view engine', 'ejs'); app.get('/', function(req, res){ res.render('default', {title: 'home', users: ['a', 'b', 'c']}); }); app.post('/me', function(req, res){ res.send("2nd page"); var t1= req.body.username; console.log(t1); });
my template:
<form method="post" enctype="multipart/form-data" action="/me"> <input type="text" name="username"> <input type="password" name="password"> <input type="submit"> </form>
i'm trying print form values in submitted page have not added jquery or in head section what's mistake?
problem form enctype, using jade template system, add "jade": "^1.6.0" in package.json , npm install
server code
var express = require('express'); var app = express(); var bodyparser= require("body-parser"); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({ extended: true })); app.set('view engine', 'jade'); app.get('/', function(req, res){ res.render('test'); }); app.post('/me', function(req, res){ console.log(req.body.username); res.send("done"); }); app.listen(8081); console.log("listening @ 8081");
views/test.jade
doctype html html form(method="post",enctype="application/x-www-form-urlencoded" action="/me") input(type="text",name="username") input(type="password" name="password") input(type="submit")
if want multipart/form-data need use
var multer = require('multer'); app.use(multer())
Comments
Post a Comment