Gaining entry to information while an HTML form is being submitted

After a 15-year break, I am diving back into web development and currently learning Node.js and ExpressJS. I have set up a registration form on index.html and now want to transfer the entered data to response.html. However, when I hit Submit, the form is posted to response.html without me being able to access the data. I can see it under Payload in Devtools, but I'm struggling to figure out how to populate the form on response.html using client-side JavaScript.

File Structure

https://i.stack.imgur.com/An48K.png

server.js

var express = require('express');
var bodyParser = require('body-parser');

var app = express();
// Create application/x-www-form-urlencoded parser  
var urlencodedParser = bodyParser.urlencoded({ extended: false });

app.use(express.static(`${__dirname}/static`));
app.get('/index.html', function (req, res) {
   res.sendFile( __dirname + "/" + "index.html" );
});

app.post('/response.html', urlencodedParser, function (req, res) {
    res.sendFile(`${__dirname}/static/response.html`);
});

var server = app.listen(8000, function () {
  var host = server.address().address;
  var port = server.address().port;
  console.log(`Server is listening onhttp://${host}:${port}`);
});

index.html

https://i.stack.imgur.com/E6XCi.png

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Registration</title>
</head>
<body>
    <h1>Registration Form</h1>
    <form action="/response.html" method="POST">  
        <label>First Name: <input type="text" name="first_name"><br></label>  
        <label>Last Name: <input type="text" name="last_name"></label>  
        <input type="submit" value="Submit">  
    </body>
</html>

response.html

https://i.stack.imgur.com/L1fM4.png

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Verify</title>
</head>
<body>
    <h1>Verify Data</h1>
    <form action="/process_final" method="POST">
        <label>First Name: <input type="text" name="first_name"><br></label>
        <label>Last Name: <input type="text" name="last_name"></label>
        <input type="submit" value="Submit">
    </body>
    <button id='getHeaders' type='button'>Get Headers</button>
    <script src='./response.js'></script>
</body>
</html>

I believe moving some processing from the server to the client side would be beneficial. While I know about templating engines like ejs and handlebars, I'm hesitant to use session storage or local storage due to potential security concerns with sensitive data. Attached are screenshots of the folder structure and HTML files alongside the server.js code.

After hours of research, I've come to the conclusion that accessing header information through client-side JavaScript may not be possible except for certain exceptions like User Agent.

Answer №1

To retrieve the information on the server side, you have the option of utilizing req.body.first_name and req.body.last_name, or alternatively, you can destructure it such as

const { firstname, lastname } = req.body

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Child functional component does not refresh after its props are altered by its parent component

Looking at the following code: MainParentComponent.js let data = 0; function MainParentComponent() { function handleClick() { data++; } return (<> <button onClick={handleClick}>Increase</button> < ...

Preserving color output while executing commands in NodeJS

My Grunt task involves shelling out via node to run "composer install". var done = this.async(); var exec = require('child_process').exec; var composer = exec( 'php bin/composer.phar install', function(error, stdout, stderr) { ...

What methods does a node server use to identify a request?

I have encountered an unusual situation while using a node server. Initially, I set up two express http node servers - server1 to render pages and server2 to handle business logic services accessed by server1. When users interact with server1 in their bro ...

Customize the keyboard on your iPod by replacing the default one with a

Looking to customize the iPOD keyboard to only display numbers, similar to a telephone keypad: https://i.stack.imgur.com/X0L3y.png The current iPOD default keyboard looks like this: https://i.stack.imgur.com/VykjU.png ...

Is it possible to manipulate an Angular #variableName in order to retrieve an ElementRef for an HTML element?

Suppose I have a scenario where I create a button like this: <button #myButton>My Button</button> ...and then use ViewChild in the following way: @ViewChild('myButton', { static: true }) createButton: ElementRef; In this case, creat ...

What is the resolution if I need to utilize a property that is untyped?

Transitioning to TypeScript from plain old JavaScript is a step I'm taking because I believe it offers significant advantages. However, one drawback that has come to light is that not all attributes are typed, as I recently discovered. For instance: ...

Modifying an item within an array of Mongoose models

I am working with a model schema that looks like this: { _id: foo cart: { items: [ { id: number name: string, } ] } } My goal is to locate the document by its id and then modify the name value of the object in ...

What factors contribute to a one-hour discrepancy between two time stamps, deviating from the anticipated value?

Let's consider the dates '2022-04-01' and '2022-05-15'. When I calculated their deviation using Chrome devtools, here are the results: https://i.stack.imgur.com/tSZvk.png The calculated result is 3801600000. However, when my frie ...

IE11 encounters an error labeled SCRIPT1010, signaling an expected Identifier when compiled

Lately, I've been encountering a strange issue in Vue.js. Here's the thing: my application runs smoothly on all browsers locally (yes, even IE 11). But when I compile it using npm run build and deploy it to my server (which essentially serves con ...

Need help fixing my issue with the try-catch functionality in my calculator program

Can someone assist me with my Vue.js calculator issue? I am facing a problem where the output gets added to the expression after using try and catch. How can I ensure that both ERR and the output are displayed in the {{output}} section? Any help would be a ...

Retrieving the initial element from the $resource.query() function in AngularJS

I am encountering an issue with the $resource.query() method. My goal is to retrieve the first element from it, but unfortunately, I am having trouble achieving this. Surprisingly, the selection controllers are able to return the collection without any hic ...

Why does the response.json() method in the Fetch API return a JavaScript object instead of a JSON string?

After using Body.json() to read the response stream and parse it as JSON, I expected to receive a JSON string when I logged the jsonData. Instead, I received a Javascript object. Shouldn't jsonData return a JSON string until we call JSON.parse() to co ...

Tips on modifying a Vue app's property externallyHere are some techniques on how

I am curious about changing the property of a vue app from an external source. I want to create a new vue app named 'app' and set 'app.propertyName' to 'someValue'. However, my attempt below did not yield the desired outcome. ...

Using jQuery and Ajax to fade in content after all images and other assets have finished loading

I've encountered an issue with loading pages via ajax for users with custom URLs. For example, a profile is usually found at http://example.com/users/Dan, but if a user has a custom URL like http://example.com/DansCustomURL, I need to fetch their desi ...

A guide on converting nested arrays within a JSON file into a table format in Google Sheets (possibly leveraging Javascript?)

After successfully connecting via OAuth2 to retrieve a JSON performance report for shares, I am curious about how to convert this object into a matrix format within Google Sheets. { "id": "ID-23035", "total_gain": 11795.72, "holdings": [ ...

What is preventing me from successfully sending form data using axios?

I've encountered an issue while consuming an API that requires a filter series to be sent within a formData. When I test it with Postman, everything works smoothly. I also tried using other libraries and had no problems. However, when attempting to do ...

Obtain the title of the text generated by clicking the button using a script function

I am working on a piece of code that generates a text box within a button's onclick function. My goal is to retrieve the name value of each text box using PHP. <script language="javascript"> var i = 1; function changeIt() ...

The JQuery datepicker fails to function properly when the input field is modified from read-only

Utilizing the datepicker in conjunction with an MVC3 application. I aim to keep the input field as readonly until triggered by an edit button. Upon focusing on the field, I want the datepicker functionality to be activated. The code snippet below functio ...

Prevent the bottom row from being sorted

I have implemented sortable table rows in my angular project, however the sorting functionality also affects some query-ui code elements. Now I am looking to exclude the last row from being sortable. HTML <div ng:controller="controller"> <ta ...

The error encountered is an unhandled rejection with a message stating "TypeError: Cannot access property 'username' of null

My tech stack includes NodeJS, PassportJS, MySQL, and Sequelize (ORM for MySQL). The following code snippet is taken from my Passport.JS file. Whenever a user registers on my website, an error is returned if the username or email is already in use. If both ...