0% found this document useful (0 votes)
12 views3 pages

Backend Exp 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Backend Exp 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

WORKSHEET-3

Student Name: Satvik kushwaha UID: 23MCA20078

Branch: MCA Section/Group: 3/A

Semester: Third Date of Performance: 11-09-2024

Subject Name: Backend -Technology Subject Code: 23CAH-705

1) Aim:
Create a Signup page and store the inputted data by user in file.

2) Code:

Server.js

const express = require('express');


const bodyParser = require('body-parser');
const fs = require('fs');

const app = express();


const PORT = 3000;

// Middleware to parse incoming form data


app.use(bodyParser.urlencoded({ extended: true }));

// Serve the signup form


app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});

// Handle form submission


app.post('/signup', (req, res) => {
const { name, email, password } = req.body;

// Create a new user object


const userData = {
name: name,
email: email,
password: password
};

// Convert user data to JSON format


const jsonData = JSON.stringify(userData) + '\n';

// Append the user data to a file


fs.appendFile('users.txt', jsonData, (err) => {
if (err) {
console.error('Error writing to file', err);
res.status(500).send('An error occurred while saving user data.');
} else {
console.log('User data saved successfully.');
res.send('Signup successful! User data has been saved.');
}
});
});

// Start the server


app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});

Index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Signup Page</title>
<!-- Inline CSS for styling -->
<style>
</head>
<body>
<div class="container">
<h2>Signup Form</h2>
<form action="/signup" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required />

<label for="email">Email:</label>
<input type="email" id="email" name="email" required />

<label for="password">Password:</label>
<input type="password" id="password" name="password" required />

<button type="submit">Sign Up</button>


</form>
<div class="footer">
<p>Already have an account? <a href="#">Login here</a></p>
</div>
</div>
</body>
</html>

3) Output:

Learning outcomes

a) Various usage of FS module. b) Body Parsing and Data Handling

You might also like