Created
September 13, 2017 17:54
-
-
Save akumbhani66/3005aa20fa0bed021e8e73e9578f0a8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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