Código
const express = require('express'); const bodyParser = require('body-parser'); const pug = require('pug'); const db = require('mongoose'); const client = require('mongodb').MongoClient; //const session = require('express-session'); var app = express(); var Schema = db.Schema; var optionsdb = { promiseLibrary: require('bluebird') }; //Conexion a MongoDB db.createConnection('mongodb://localhost/project', optionsdb); //Creacion de la Base de Datos en un JSON var usuarios = { email: String, password: String, } /* cookies app.use(session({ secret: 'a4f8071f-c873-4447-8ee2', cookie: { maxAge: 2628000000 }, store: new (require('express-sessions'))({ storage: 'mongodb', instance: db, // optional host: 'localhost', // optional port: 27017, // optional db: 'project', // optional collection: 'users', // optional expire: 86400 // optional }) })); */ //no tocar var db1; //Model var user_schema = new Schema(usuarios); var User = db.model('User', user_schema); //vistas y configuracion app.set('view engine', 'pug') app.use(bodyParser.urlencoded({ extended: true })) app.use(express.static('public')); app.use(bodyParser.json()); //Conexion de MongoDB al cliente client.connect('mongodb://localhost/project', (err, database) => { // Chequeo de Error en Conexion if (err) return console.log(err); db1 = database; //peticiones HTTP app.get('/', (req,res) => { res.render('index'); console.log('Hiciste una peticion GET'); }); app.post('/', (req,res) => { datos = new User({ email: req.body.email, password: req.body.password }) datos.save( (err,datos)=> { console.log("Usuario registrado") res.render("bienvenida") }) }) //Puerto en el que se va a escuchar el servidor app.listen(80, 'localhost', () => { console.log('Servidor iniciado correctamente'); }); });
Ahora, cuando realizo la peticion post en] el formulario (Hecho con pug):
Código
extends ./layouts.pug block contenido body div(class="container") form(action='/', method="POST", class="form-signin") .container form.form-signin h2.form-signin-heading Please sign in label.sr-only(for='inputEmail') Email address input#inputEmail.form-control(type='email', placeholder='Email address', ref="email", name="email" required, autofocus='') label.sr-only(for='inputPassword') Password input#inputPassword.form-control(type='password', ref="password", placeholder='Password', required, name="password") .checkbox label input(type='checkbox', value='remember-me') | Remember me button.btn.btn-lg.btn-primary.btn-block(type='submit') Sign in
no me envia los datos a la base de datos, ni me redirecciona a la pagina de bienvenida:
Código
extends ./layouts.pug block contenido body div.container div.row div.col-md-12 div.card h1 Registrado
Sin mas, en la consola me sale este error:
(node:5932) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html y la peticion se queda cargando asi no mas.