Created
June 25, 2017 16:31
-
-
Save anonymous/a84e505f89e694d6a5b750d7c889328f 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
var express = require('express'); | |
var MongoClient = require('mongodb').MongoClient; | |
var router = express.Router(); | |
var fs = require('fs'); | |
var products = []; | |
var mongoose = require('mongoose'); | |
var Schema = mongoose.Schema; | |
var productsSchema = new Schema({ | |
title: String, | |
price: Number | |
}); | |
var products = mongoose.model('products', productsSchema); | |
; | |
mongoose.connect('mongodb://localhost/products'); | |
router.get('/create', function (req, res, next) { | |
res.render('product/create', { title: 'Express' }); | |
}); | |
router.get('/products', function (req, res) { | |
console.log('getting all productss'); | |
products.find({}) | |
.exec(function (err, products) { | |
if (err) { | |
res.send('errror'); | |
} else { | |
console.log(products); | |
res.json(products); | |
} | |
}) | |
} | |
); | |
//========================CREATE PRODUCT==========================// | |
router.post('/create', function (req, res, next) { | |
var item = { | |
title: req.body.name, | |
price: req.body.price | |
} | |
var data = new ProductData(item); | |
data.save(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment