Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
How to connect backend create user function to front end (Node.js, React.js, MongoDB)

I am struggling in figuring out how to have a front-end that corresponds to my back-end in creating a user. I've successfully managed to create a user (can post to MongoDB using Insomnia) but don't know what's the structure for creating a user in the front end. Here's what my code looks like:

Router

const express = require('express');
const router = express.Router();

router.post('/', async (req, res) => {
    // First Validate The Request
    const { error } = validate(req.body);
    if (error) {
        return res.status(400).send(error.details[0].message);
    }

    // Check if this user already exisits
    let user = await User.findOne({ email: req.body.email });
    if (user) {
        return res.status(400).send('That user already exisits!');
    } else {
        // Insert the new user if they do not exist yet
        user = new User({
            name: req.body.name,
            email: req.body.email,
            password: req.body.password
        });
        await user.save();
        res.send(user);
    }
});

module.exports = router;

server.js

app.use('/api/users', users);

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Post request to this the API:

import axios from 'axios'

axios({
  method: 'post',
  url: '/api/users',
  data: {
    name: 'John',
    email: 'john@example.com',
    password: 'Something'
  }
});
about 4 years ago · Santiago Trujillo Report

0

You can use axios for example, an popular library: https://github.com/axios/axios

axios.post("URL", { email: "myemail@mail.com" })

about 4 years ago · Santiago Trujillo Report

0

What you should do, is store information the user types in your frontend in a variable and pass this data to the fetch's function via the post method.

// User information
let username = 'boneless';

// Options for your request
let options = { method: 'POST', body: JSON.stringify({username: username});

let request = await fetch('url', { options });

let response = await request;

// Now you can apply logic to your response. Based on how your server replies.

I recommend reading up on fetch, as it's native in most browsers.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!