0% found this document useful (0 votes)
4 views4 pages

Code

This document provides a complete example of a web application using Node.js and Express.js to handle user sign-up. It includes server-side code to connect to a MySQL database and insert user data, as well as client-side HTML and JavaScript to submit the sign-up form. The MySQL database setup is also detailed, including the creation of a 'users' table.

Uploaded by

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

Code

This document provides a complete example of a web application using Node.js and Express.js to handle user sign-up. It includes server-side code to connect to a MySQL database and insert user data, as well as client-side HTML and JavaScript to submit the sign-up form. The MySQL database setup is also detailed, including the creation of a 'users' table.

Uploaded by

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

Here's an updated example that includes server-side code using [Link] and Express.

js, and connects to


a MySQL database:

Server-Side Code ([Link] and [Link]):

Create a new file called `[Link]` and add the following code:

```

const express = require('express');

const mysql = require('mysql');

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

const app = express();

// Connect to MySQL database

const db = [Link]({

host: 'localhost',

user: 'your_username',

password: 'your_password',

database: 'your_database'

});

[Link]((err) => {

if (err) {

[Link]('error connecting:', err);

return;

[Link]('connected as id ' + [Link]);

});
// Use body-parser middleware to parse JSON requests

[Link]([Link]());

// Define route for sign-up form submission

[Link]('/signup', (req, res) => {

const { name, email, password } = [Link];

// Validate form data

if (!name || !email || !password) {

[Link](400).send({ message: 'Please fill in all fields.' });

return;

// Insert user data into MySQL database

const query = 'INSERT INTO users SET ?';

const userData = { name, email, password };

[Link](query, userData, (err, results) => {

if (err) {

[Link]('error inserting user:', err);

[Link](500).send({ message: 'Error creating user.' });

} else {

[Link]({ message: 'User created successfully.' });

});

});

// Start [Link] server

const port = 3000;

[Link](port, () => {
[Link](`Server listening on port ${port}`);

});

```

MySQL Database Setup:

Create a new MySQL database and add a `users` table with the following columns:

```

CREATE TABLE users (

id INT PRIMARY KEY AUTO_INCREMENT,

name VARCHAR(255),

email VARCHAR(255) UNIQUE,

password VARCHAR(255)

);

```

Client-Side Code (HTML, CSS, and JavaScript):

Update the HTML, CSS, and JavaScript code to send a POST request to the `/signup` endpoint:

*HTML:*

```

<form id="signup-form">

<label for="name">Name:</label>

<input type="text" id="name" name="name"><br><br>

<label for="email">Email:</label>

<input type="email" id="email" name="email"><br><br>

<label for="password">Password:</label>

<input type="password" id="password" name="password"><br><br>

<input type="submit" value="Sign Up">


</form>

```

*JavaScript:*

```

const form = [Link]('signup-form');

[Link]('submit', (e) => {

[Link]();

const name = [Link]('name').value;

const email = [Link]('email').value;

const password = [Link]('password').value;

const userData = { name, email, password };

fetch('/signup', {

method: 'POST',

headers: { 'Content-Type': 'application/json' },

body: [Link](userData)

})

.then((res) => [Link]())

.then((data) => [Link](data))

.catch((err) => [Link](err));

});

```

This updated example includes server-side code using [Link] and [Link], connects to a MySQL
database, and sends a POST request from the client-side JavaScript code to the `/signup` endpoint.

You might also like