I'm trying to submit Customer data to the server for processing. I'm using an HTML form. But every time I press submit the server doesn't receive any data in the request.
<form data-customer-information-form="true" autocomplete="on" method="POST" action="addticket/submit" name="ticketForm" id="ticketform" accept-charset="UTF-8">
<p>
<label for="customerFirstName">First Name:</label></br>
<input type="text" name="customerFirstName" id="customerFirstName" placeholder="first name" pattern="[A-Za-z]+" tabindex="0">
</p>
<p>
<label for="customerLastName">Last Name:</label></br>
<input type="text" name="customerLastName" id="customerLastName" placeholder="last name" tabindex="0">
</p>
<p>
<label for="phoneNumber">Phone:</label></br>
<input type="tel" id="phone" maxlength="12" name="phoneNumber" placeholder="phone" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" tabindex="0">
</p>
<p>
<label for="email">Email:</label></br>
<input type="email" name="email" id="email" placeholder="email">
</p>
<p>
<label for="service">Computer/Service Name:</label></br>
<input type="text" name="service" placeholder="computer model or service">
</p>
<p>
<label for="description">Anything else we need to know:</label></br>
<textarea type="text" maxlength="200" name="description" id="description" placeholder="What's gone wrong?"></textarea>
</p>
<input type="submit" name="submit" value="Submit" id="submit">
</form>
const router = require('express').Router();
const fs = require('fs');
var path = require('path');
const customer = require("../models/customer");
router.get('/', function(req, res) {
req.session.cookie
res.sendFile(path.join(__dirname + "/pages/ticket.html"));
});
router.post('/submit', (req, res) => {
console.log(req.body)
})
module.exports = router;
The request headers all look good telling the server that is a form. But it doesn't have the data. req.body is undefined basically.
Express.js will only populate the body
property of a request object if suitable body parsing middleware is used. You aren't using any.
By default, a form will encode form data in the URL encoded format, so you should use that middleware.
const express = require('express');
router.use(express.urlencoded())