@@ -2445,18 +2445,114 @@ var server = app.listen(3000, function () {
24452445 <b><a href="#">↥ back to top</a></b> 
24462446</div >
24472447
2448- #### Q. *** What is the difference between mysql.createConnection() and mysql.createPool() in Node.js MySQL module?***   
2449- #### Q. *** how to handle file upload in node js?***   
2448+ ## Q. *** How to handle file upload in Node.js?***   
2449+ 
2450+ *  ** express** : Popular web framework built on top of Node.js, used for creating REST-API.
2451+ *  ** body-parser** : Parse incoming request bodies in a middleware
2452+ *  ** multer** : Middleware for handling multipart/form-data — file uploads
2453+ 
2454+ ** 1. Installing the dependencies** 
2455+ 
2456+ ``` bash 
2457+ npm install express body-parser multer --save
2458+ ``` 
2459+ 
2460+ ** 2. Package.json** 
2461+ 
2462+ ``` json 
2463+ {
2464+  "name" : " file_upload"  ,
2465+  "version" : " 0.0.1"  ,
2466+  "dependencies" : {
2467+  "express" : " 4.13.3"  ,
2468+  "multer" : " 1.1.0" 
2469+  },
2470+  "devDependencies" : {
2471+  "should" : " ~7.1.0"  ,
2472+  "mocha" : " ~2.3.3"  ,
2473+  "supertest" : " ~1.1.0" 
2474+  }
2475+ }
2476+ ``` 
2477+ 
2478+ ** 3. Server.js** 
2479+ 
2480+ ``` js 
2481+ var  express =  require (" express"  );
2482+ var  bodyParser =  require (' body-parser'  );
2483+ var  multer =  require (' multer'  );
2484+ var  app =  express ();
2485+ 
2486+ //  for text/number data transfer between clientg and server
2487+ app .use (bodyParser ());
2488+ 
2489+ var  storage =  multer .diskStorage ({
2490+  destination :  function  (req , file , callback ) {
2491+  callback (null , ' ./uploads'  );
2492+  },
2493+  filename :  function  (req , file , callback ) {
2494+  callback (null , file .fieldname  +  ' -'   +  Date .now ());
2495+  }
2496+ });
2497+ 
2498+ var  upload =  multer ({ storage :  storage}).single (' userPhoto'  );
2499+ 
2500+ app .get (' /'  , function (req , res ) {
2501+  res .sendFile (__dirname  +  " /index.html"  );
2502+ });
2503+ 
2504+ //  POST: upload for single file upload
2505+ app .post (' /api/photo'  , function (req , res ) {
2506+  upload (req,res,function (err ) {
2507+  if (err) {
2508+  return  res .end (" Error uploading file."  );
2509+  }
2510+  res .end (" File is uploaded"  );
2511+  });
2512+ });
2513+ 
2514+ app .listen (3000 , function (){
2515+  console .log (" Listening on port 3000"  );
2516+ });
2517+ ``` 
2518+ 
2519+ ** 4. index.html** 
2520+ 
2521+ ``` html 
2522+ <!DOCTYPE html>
2523+ <html  lang =" en"  >
2524+ <head >
2525+  <title >Multer-File-Upload</title >
2526+ </head >
2527+ <body >
2528+  <h1 >MULTER File Upload | Single File Upload</h1 > 
2529+ 
2530+  <form  id  = " uploadForm" 
2531+  enctype  = " multipart/form-data" 
2532+  action  = " /api/photo" 
2533+  method  = " post" 
2534+  >
2535+  <input  type =" file"   name =" userPhoto"   />
2536+  <input  type =" submit"   value =" Upload Image"   name =" submit"  >
2537+  </form >
2538+ </body >
2539+ </html >
2540+ ``` 
2541+ 
2542+ <div  align =" right " >
2543+  <b><a href="#">↥ back to top</a></b> 
2544+ </div >
2545+ 
24502546#### Q. *** Explain the terms body-parser, cookie-parser, debug, jade, morgan, nodemon, pm2, serve-favicon, cors, .env, checksum, fs-extra, moment in Express JS?***   
24512547#### Q. *** How does routing work in Node.js?***   
24522548#### Q. *** How Node prevents blocking code?***   
2453- #### Q. *** What is difference between promise and async await in node  js?***   
2454- #### Q. *** How to use JSON Web Token (JWT) for authentication in node  js?***   
2455- #### Q. *** How to build a microservices architecture with node  js?***   
2456- #### Q. *** How to use Q promise in node  js?***   
2457- #### Q. *** How to use locale (i18n) in node  js?***   
2458- #### Q. *** How to implement Memcached in Nodejs ?***   
2459- #### Q. *** Explain Error Handling in node  js?***   
2549+ #### Q. *** What is difference between promise and async await in Node. js?***   
2550+ #### Q. *** How to use JSON Web Token (JWT) for authentication in Node. js?***   
2551+ #### Q. *** How to build a microservices architecture with Node. js?***   
2552+ #### Q. *** How to use Q promise in Node. js?***   
2553+ #### Q. *** How to use locale (i18n) in Node. js?***   
2554+ #### Q. *** How to implement Memcached in Node.js ?***   
2555+ #### Q. *** Explain Error Handling in Node. js?***   
24602556
24612557<div  align =" right " >
24622558 <b><a href="#">↥ back to top</a></b> 
0 commit comments