×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Back on the Node(ExpressJS) fetch API from WEB Browser

Back on the Node(ExpressJS) fetch API from WEB Browser

Back on the Node(ExpressJS) fetch API from WEB Browser

(OP)
Hi all, can someone explain to me, how does communicate frontend and backend? I have a project, that dividing into frontend and backend folders. And I can't understand how can I send the data from the registration form to the server-side using Fetch API. I want to create a SPA.
Thank you in advance, and sorry for my English.
Best regards!
P.s: The structure of my project in the attached files.
This is the code on the frontend:

CODE --> JavaScript

const formData = {
                        name: name.value,
                        age: age.value.split('-').join('.'),
                        email: email.value,
                        password: pass.value,
                    };

                const checkResponseStatus = function(res) {
                    if (res.ok) {
                        return res;
                    } else {
                        throw new Error(`The HTTP status of the response: ${ res.status } (${ res.statusText })`);
                    }
                };

                const url = '/register';
                const request = new Request(url, {
                        method: 'POST',
                        body: JSON.stringify(formData),
                        headers: { 'Content-Type': 'application/json' }
                    });

                fetch(request)
                    .then(checkResponseStatus)
                    .then(response => response.json())
                    .catch(err => console.log(err));

                name.value = '';
                age.value = '';
                email.value = '';
                pass.value = '';
                repeatPass.value = '';

                for (let key of Object.keys(formData)) {
                    delete formData[key];
                } 
And this is the code on the backend (server.js):

CODE --> JavaScript

const express = require('express');
const bodyParser = require('body-parser');
const router = express.Router();

const app = express();

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

app.use((req, res) => {
	const post_data = req.body;
	console.log(post_data);
});

router.get('/', (req, res) => {
	res.sendFile('../frontend/index.html');
});

router.post('/register', (req, res) => {
	const user_name = req.body.name;
	const password = req.body.password;
	console.log(`User name: ${ user_name }, password: ${ password }`);
	res.end('YES');
});

app.listen(3000, () => {
	console.log(`Started on PORT 3000`);
}); 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close