text: const express = require('express');
  const router = express.Router();
  const multer = require('multer');
  const fs = require('fs');
  const mongoose = require('mongoose');
  
  const image = require('../Models/Image.js');
  const Product = require('../Models/Product');
  
  const fileFilter = (req, file, cb) => {
      // reject a file
      if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
          cb(null, true);
      } else {
          cb(null, false);
      }
  };
  
  var storage = multer.diskStorage({
      destination: function (req, file, cb) {
          cb(null, './uploads/')
      },
      filename: function (req, file, cb) {
  
          cb(null, Date.now()+file.originalname);
      }
  });
  const upload = multer({ storage: storage });
  
  router.post('/upload', upload.single('myImage'), (req, res, next) => {
      var img = fs.readFileSync(req.file.path);
      var encode_img = img.toString('base64');
      var final_img = {
          contentType:req.file.mimetype,
          image:new Buffer(encode_img,'base64')
      };
      console.log(final_img);
      image.create(final_img,function(err,result){
          if(err){
              console.log(err);
          }else{
              console.log(result.img.Buffer);
              console.log("Saved To database");
              res.contentType(final_img.contentType);
              res.send(final_img.image);
          }
      }); });
  
  router.post("/", upload.single('productImage'), (req, res, next) => {
    console.log(req.file);
    const url = req.file.path.replace(/\\/g,'/');
     const product = new Product({
          _id: new mongoose.Types.ObjectId(),
          name: req.body.name,
          price: req.body.price,
          productImage: url
      });
      product
          .save()
          .then(result => {
              console.log(result);
              res.status(201).json({
                  message: "Created product successfully",
                  createdProduct: {
                      name: result.name,
                      price: result.price,
                      _id: result._id,
                      request: {
                          type: 'GET',
                          url: "http://localhost:3000/image/" + result._id
                      }
                  }
              });
          })
          .catch(err => {
              console.log(err);
              res.status(500).json({
                  error: err
              });
          });
  });
  router.get("/:productId", (req, res, next) => {
      const id = req.params.productId;
      Product.findById(id)
          .select('name price _id productImage')
          .exec()
          .then(doc => {
              console.log("From database", doc);
              if (doc) {
                  res.status(200).json({
                      product: doc,
                      request: {
                          type: 'GET',
                          url: 'http://localhost:3000/image'
                      }
                  });
              } else {
                  res
                      .status(404)
                      .json({ message: "No valid entry found for provided ID" });
              }
          })
          .catch(err => {
              console.log(err);
              res.status(500).json({ error: err });
          });
  });
  router.get("/", (req, res, next) => {
      Product.find()
          .select("name price _id productImage")
          .exec()
          .then(docs => {
              const response = {
                  count: docs.length,
                  products: docs.map(doc => {
                      return {
                          name: doc.name,
                          price: doc.price,
                          productImage: doc.productImage,
                          _id: doc._id,
                          request: {
                              type: "GET",
                              url: "http://localhost:3000/image/" + doc._id
                          }
                      };
                  })
              };
              //   if (docs.length >= 0) {
              res.status(200).json(response);
              //   } else {
              //       res.status(404).json({
              //           message: 'No entries found'
              //       });
              //   }
          })
          .catch(err => {
              console.log(err);
              res.status(500).json({
                  error: err
              });
          });
  });
  module.exports = router;