tried to add image from admin panel ,through form submission to the folder product-images inside public folder.The image not added to folder.Nodejs

Viewed 14

in this ecommercial website i had tried to add image to a folder called product-image inside public folder but when i try localhost:3000/admin/add-product the form submission of image is not added to the product-image folder in the project.Error: "This site can’t be reached localhost refused to connect." when submit button is pressed..... pls help me....

admin.js

 var express = require('express');
 const productHelpers = require('../helpers/product-helpers');
 var router = express.Router();
 var productHelper=require('../helpers/product-helpers')

  /* GET users listing. */
  router.get('/', function(req, res, next) {

  let products=[
  {
  name:"Hero Kyoto",
  category:'bicycle',
  description:'This is a good phone',
  image:"https://m.media-amazon.com/images/I/71CA2d53yML._SX522_.jpg"
  },
  {
  name:"Hero Kyoto",
  category:'bicycle',
  description:'This is a good phone',
  image:"https://m.media-amazon.com/images/I/71CA2d53yML._SX522_.jpg"
  },
  {
  name:"Hero Kyoto",
  category:'bicycle',
  description:'This is a good phone',
  image:"https://m.media-amazon.com/images/I/71CA2d53yML._SX522_.jpg"
  },
  {
  name:"Hero Kyoto",
  category:'bicycle',
  description:'This is a good phone',
  image:"https://m.media-amazon.com/images/I/71CA2d53yML._SX522_.jpg"
  }
  ]

   res.render('admin/view-products',{admin:true,products});
   });

  router.get('/add-product',function(req,res){
   res.render('admin/add-product')

   })
  router.post('/add-product',(req,res)=>{

   productHelpers.addProduct(req.body,(id)=>{
   let image=req.files.Image;
   console.log(id);
   image.mv('../public/product-images/'+id+'.jpg',(err,done)=>{
   if(!err){
    res.render("admin/add-product")

    }else{
    console.log(err);
    }
    })
 
    })
    })

   module.exports = router;

product-helpers.js

   var db=require('../config/connection')
   module.exports = {
   addProduct: (product, callback) => {
   db.get().collection('product').insertOne(product).then((data) =>{
   callback(data.ops[0]._id)
    })
   }
   }

connection.js

    const mongoClient=require('mongodb').MongoClient
    const state={
    db:null
    }
   module.exports.connect=function(done){
   const url='mongodb://localhost:27017'
   const dbname='shopping'

   mongoClient.connect(url,(err,data)=>{
    if(err) return done(err)
    state.db=data.db(dbname) 
    done()
   })   
   }

   module.exports.get=function(){
   return state.db
   }
0 Answers
Related