Sunday, 23 April 2017

Node.js and MongoDB interfacing

We have a CSS form.How do we pass data from one of the text boxes, as a query to the DataBase? Further if the result of the query is NULL, we want to re-direct to another page.If not, we want to retain the same webpage.

This is what we have done so far:

const express = require('express');
const app = express();
const bodyParser= require('body-parser')


const MongoClient = require('mongodb').MongoClient
var db

MongoClient.connect('mongodb://127.0.0.1:27017/student_database',                  (err, database) => {
 if (err) return console.log(err)
 db = database
 app.listen(3000, () => {
 console.log('listening on 3000')
 })
 }) 
 app.get('/', (req, res) => {
 res.sendFile('/Program Files/MongoDB'+'/login.html')
 //db.collection('Profile').find({},function(err, results) {
 //if(err)  res.json(err);
 //else res.send('');
 //console.log(results)
//})

})
app.use(bodyParser.urlencoded({extended:true}))
app.post('/login', (req, res) => {
 db.collection('Profile').find({_id:req.body._id},function(err,             results) {
  if(results)
    console.log(results)
   else
    res.sendFile('/Program Files/MongoDB'+'/profile.html')
 }) 
 })



via 204

No comments:

Post a Comment