Hello all, I am in the process of building my first full stack application and wanted to know the best way to see what I am passing to the back-end. Just to be clear on what I am asking, I am trying to log the data that I send from the front-end in my command line. I want to be able to see whatever snippet of code that I sent in the request instead of seeing all the information about the request.
I am using HTML, CSS, Javascript, Express.js and mysql for this full stack application.
Below I have provided the code used on the front-end for the request as well as the server-side code.
Client-side code
function sendMessage(){
const message = document.getElementById("message");
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(this.readyState ===4 && this.status === 200){
document.getElementById("message").innerText = this.responseText;
}
}
xhttp.open("POST",`http://localhost:2400/example`,true);
xhttp.send();
}
sendMessage();
The Server-side Code
const express = require("express");
const mysql = require("mysql");
//const bodyParser = require("body-parser");
//activates express
const app = express();
//incorporates body parser into our app so we can read json data sent from the front end
const urlencodedParser = bodyParser.urlencoded({ extended: false })
const jsonParser = bodyParser.json();
//creates a port for the server to listen to on the front end
const port = 2400;
app.listen(2400, function () {
console.log(`Server started on port ${port}`);
});
//testing a simple POST method made by some front-end javascript code
app.post("/example", function (req, res) {
console.log(req);
});