Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSiavash Gheifardi
12,510 PointsMaking a POST request for a form using node.js
Been trying to learn node and i'm having a terrible time. I dont know what i'm doing wrong but all i want is the input that the visitor submits to show in my console.
<!DOCTYPE html>
<html>
<head>
<title>Electric</title>
<meta charset = "utf-8">
<link rel="stylesheet" type="text/css" href="css/style.css">
<link href='https://fonts.googleapis.com/css?family=Josefin+Sans|Kaushan+Script' rel='stylesheet' ty pe='text/css'>
</head>
<body>
<form id="testimonial form" method = "POST" action = "/profile">
<label for="firstname">First Name</label>
<input type="text" id="firstname" name="firstname">
<label for="lastname">Last Name</label>
<input type="text" id="lastname" name="lastname"> </br></br>
<label for="address">Street Address</label>
<input type="text" id="address" name="address"></br></br>
<label for="address2">Street Address Line 2</label>
<input type="text" id="address2" name="address2"></br></br>
<label for="city">City</label>
<input type="text" id="city" name="city">
<label for="state/province">State or Province</label>
<input type="text" id="state/province" name="state/province"></br></br>
<label for="postal/zipcode">Postal/Zipcode</label>
<input type="text" id="postal/zipcode" name="postal/zipcode">
<label for="country">Country</label>
<input type="text" id="country" name="country"></br></br>
<label for="email">Email Address</label>
<input type="text" id="email" name="email address"></br></br>
Testimonial:</br>
<textarea rows="10" cols="40"></textarea></br></br>
<input type="submit" value="Submit">
</form>
<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/java.js"></script>
</body>
and this is my node file.
var express = require("express");
var bodyParser = require('body-parser')
var app = express();
var urlencodedParser = bodyParser.urlencoded({ extended: false})
app.set('view engine', 'ejs');
app.use('/assets', express.static('stuff'));
app.get('/', function(req,res){
res.render('index')
});
app.get('/profile', function(req, res){
res.render('contact', {qs: req.query});
});
app.post('/profile', urlencodedParser, function(req, res){
res.render('contact', {qs: req.query});
console.log(req.body);
});
Can someone help me please. The threehouse videos dont really do much for me.
2 Answers
Patrick McKinney
13,151 PointsI haven't worked with Node much, but there might be an issue with the form element:
<form id="testimonial form" method = "POST" action = "/profile">
I'm not sure if you can have spaces between attributes and values. There also shouldn't be a space for the id of the form.
Perhaps making this change will help?
<form id="testimonialForm" method="POST" action="/profile">
Maryann Sailer
3,002 PointsTry moving the console log above the res.send()