How to send data through a post request in NodeJS to the database in MongoDB?

1

I'm doing an App, which has a registry for users. I try to make that when the user enters the data they are sent to the database through that POST request. The database appears connected to me, but I can not find the code to send the data.

This is my server:

const express = require("express");
const bodyParser = require("body-parser");
const pug = require("pug");
const data = require("mongodb").MongoClient;
const app = express();
const mongoose = require('mongoose');
//var model = mongoose.model('algunModelo, algunSchema');
var Schema = mongoose.Schema;

var users = Schema({
  email: String,
  password: String
})
var db;

app.set("view engine", "pug");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.use(bodyParser.json());

data
  .connect("mongodb://localhost:27017/project", (err, database) => {
    db = database;
    var user = db.collection("users"); //haciendo referencia a la conexion 
    if (err) return console.log(err);
    //peticiones HTTP
    app.get("/", (req, res) => {
      res.render("index");
      console.log("Hiciste una peticion GET");
    });
    app.get("/register", (req, res) => {
      res.render("register");
      console.log("Entraste al Register");
    });

    app.post("/home", (req, res) => {
      console.log("Has accedido al registro");
      db.save({
        email : req.body.email,
        password : req.body.password
      }, err => {
        if(err) throw err
        else
          console.log("Datos enviados \n al servidor");
          res.render('home');
      });
    });
    if (err) throw err;
    else return console.log("Base de datos conectada")
  });

  app.listen(80, "localhost", err => {
    if (err) throw err;
    console.log("Ya se conectooooo")
  });

and this is the view where I have the registration form, of course in pug:

extends ./layouts.pug
block contenido
  body
    .container
      .row
      .row
      .row
      .row
      .row
      .row
        .col-md-4
        .col-md-5.col-md-push-3
          a.btn.btn-deep-purple(href="/")
            i(class="fa fa-arrow-left", aria-hidden="true")  
              | back home
          a.btn.btn-deep-purple.fa(href="/login")   Login
            |  
            i(class="fa fa-arrow-right", aria-hidden="true")
      .row
      .row  
      .row
        .col-md-3
        .col-md-6.col-md-push-3
          .card
            .card-block
              .text-center
                h3
                  i.fa.fa-lock
                  |  Register
                hr.mt-2.mb-2
              form(action='/home', method='POST')
                .md-form
                  i.fa.fa-envelope.prefix
                  input#form2.form-control(type='text')
                  label(for='form2') Your email
                .md-form
                  i.fa.fa-lock.prefix
                  input#form4.form-control(type='password')
                  label(for='form4') Your password
                .text-center
                  button.btn.btn-deep-purple(type="submit")  Sign up
                  div
                    a#google-connect.social-button(href='#')
                      span Sign up with Google
                    a#twitter-connect.social-button(href='#')
                      span Sign up with Twitter
          br
          .card
            .card-block
              p Made by Diesan Romero © 2017. All rights reserved.
          // /Form without header
  script(src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js')
  script(src="https://cdnjs.cloudflare.com/ajax/libs/mdbootstrap/4.3.0/js/mdb.min.js")
    
asked by Diesan Romero 23.06.2017 в 17:34
source

0 answers