Advertisement
xapu

Untitled

Oct 3rd, 2017
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const http = require('http')
  2. const url = require('url')
  3. const fs = require('fs')
  4. const multiparty = require('multiparty')
  5.  
  6. let uploadFunc = (req, resp) => {
  7.   req.pathname = req.pathname || url.parse(req.url).pathname
  8.  
  9.   if (req.pathname === '/upload' && req.method === 'POST') {
  10.     let form = new multiparty.Form()
  11.     let product = {}
  12.  
  13.     form.uploadDir = './'
  14.  
  15.     console.log(form)
  16.  
  17.     form.parse(req, function (err, fields, files) {
  18.       resp.writeHead(200, {
  19.         'Contetn-Type': 'text/plain'
  20.       })
  21.       resp.end('Uploaded')
  22.     })
  23.   } else {
  24.     resp.writeHead(200, {
  25.       'Content-Type': 'text/html'
  26.     })
  27.  
  28.     resp.end(
  29.       '<form action="/upload" enctype="multipart/form-data" method="post">' +
  30.         '<input type="text" name="title"><br>' +
  31.         '<input type="file" name="upload" multiple="multiple"><br>' +
  32.         '<input type="submit" value="Upload">' +
  33.         '</form>'
  34.     )
  35.   }
  36. }
  37.  
  38. http
  39.   .createServer((req, res) => {
  40.     uploadFunc(req, res)
  41.   })
  42.   .listen(3333)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement