I have a basic express set up. I'm trying to do something which on tutorials seems pretty simple but is not working at all for me. I'm attempting to capture the data entered by a user in two inputs, a text input and a dropdown input.
My app.js file looks like this:
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var pug = require('pug');
// initialize express
var app = express();
// set up port
var port = process.env.PORT || 1337;
// setting up bodyParser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true}));
// setting up views
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(express.static(path.join(__dirname, 'public')));
// initial homepage get
app.get('/', function(req, res) {
res.render('index');
});
app.post('/', function(req, res) {
var input = req.body.userInput;
var quantity = req.body.quantity;
console.log("input is: " + input);
console.log("quantity is: " + quantity);
});
// listening on port
app.listen(port, function(err) {
if (err) throw err;
console.log("Server is running on port " + port);
});
My pug file form looks like this:
.card
.card-body
form(action='/', method="POST")
.form-row
input.form-control(type='text', placeholder="Search for an item by its id", name="userInput")
.form-row.mt-2
.form-group
select.form-control(name="quantity")
option(value='1') Qty: 1
option(value='2') Qty: 2
option(value='3') Qty: 3
option(value='4') Qty: 4
option(value='5') Qty: 5
.card-footer
button.btn.btn-outline-secondary(type='submit')
i.fas.fa-shopping-cart.mr-2
| Add to my cart
Everything renders properly. I gave a "name" field to each form input I have and I try to grab that data from req.body.(name) in the app.js file. But when I run it and input data, nothing shows up in the console log.
console.log(req.body)?