Skip to content

Instantly share code, notes, and snippets.

@akumbhani66
Created September 13, 2017 17:54
Show Gist options
  • Save akumbhani66/3005aa20fa0bed021e8e73e9578f0a8d to your computer and use it in GitHub Desktop.
Save akumbhani66/3005aa20fa0bed021e8e73e9578f0a8d to your computer and use it in GitHub Desktop.
const express = require('express');
const bodyParser = require('body-parser');
const app = new express();
var DB;
var MongoClient = require('mongodb').MongoClient
MongoClient.connect('mongodb://localhost:27017/mytest', function (err, db) {
if (err) throw err
DB = db;
db.collection('crud').find().toArray(function (err, result) {
if (err) throw err
console.log(result)
})
})
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}))
app.listen(4000);
console.log("port 4000");
app.get("/", function(req, res, next){
res.json({"hi": "ashvin"});
});
app.post("/post", function(req, res, next){
console.log(req.body);
DB.collection('crud').save(req.body, (err, result) => {
if (err) return console.log(err)
console.log('saved to database')
res.redirect('/')
})
res.json(req.body);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment