Saving Data Into MongoDB (with using mongoose) When HTML Button Clicked

I’m making a basic blog app and I want to save users’ data when they click submit button. I’m using mongoose for saving data in MongoDB. I wrote some code but it doesn’t worked. I’m new to this website things and I’m trying to learn.

There is code that I wrote for save into DB:

const mongoose = require("mongoose");
const User = require("../models/user");

const db_URI = 'my db_uri in here, idk about security so i didn't write it'; 
const button = document.getElementById("submit-btn");

mongoose.connect(db_URI)

button.onclick = function() {
    var user = new User( { # I made this variable for just test
        name:"qwe",
        password: "123",
        email: "example@hotmail.com"
    }); 

    user.save()
};

This code is for my front-end part:

<!DOCTYPE html>
<html>
    <head>
        <title>SIGN UP HERE</title>

    </head>


    <body>
        <form method="POST">
            <div>
                <label id="name-lbl">Name: </label>
                <input type="text" name="nick" placeholder="Your nickname here">
            </div>
            <div>
                <label>E-mail: </label>
                <input type="text" name="email" placeholder="Your email here">
            </div>
            <div>  
                <label>Password: </label>
                <input type="password" name="pwd" placeholder="Your password here">
            </div>
            <div>
                <label>Confirm password: </label>
                <input type="password" name="pwd" placeholder="Confirm your password">
            </div>
            <button type="submit" id="submit-btn">Submit</button>
            
        </form>
        
        <script src="../database/db_processes.js"></script>
    </body>
</html>

Answer

You are mixing up some things here. Your backend application (your first code snippet) should be running “server side” using Node. How are you running it now? Also the first code snippet would not have access to the frontend components, like buttons and divs.

What I would suggest is to go through this tutorial: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs to learn the basics of the architecture(Client-server architecture). In part 3 they go through how to connect with Mongoose.