My problem is that my POST logger is not logging the requests from my javascript. Here is the code for each:
-
Client Side
// JavaScript code to handle form submission const form = $("#register-form"); form.submit(function(event) { event.preventDefault(); // prevent the form from submitting normally const formData = form.serialize(); // serialize the form data $.ajax({ url: "http://localhost:3000", method: "POST", data: formData, success: function(response) { // the response was successful console.log("Login successful!"); }, error: function(xhr, status, error) { // an error occurred while sending the request console.error(error); } }); });
2.Server Side
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
// handle POST request
app.post('/', (req, res) => {
console.log(req.body);
res.send('Received POST request');
});
app.listen(3000, () => {
console.log('Server listening on port 3000');
});
I had checked the server with curl, and I think that’s a client side issue. Cheers!