Skip to content

Instantly share code, notes, and snippets.

Created June 25, 2017 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/a84e505f89e694d6a5b750d7c889328f to your computer and use it in GitHub Desktop.
Save anonymous/a84e505f89e694d6a5b750d7c889328f to your computer and use it in GitHub Desktop.
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