Geen omschrijving

server.js 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var express = require('express');
  2. var bodyParser = require('body-parser');
  3. var multer = require('multer');
  4. var upload = multer({dest : 'uploads/'});
  5. var app = express();
  6. var fs = require('fs');
  7. app.listen(8123, function(err){
  8. if(!err)
  9. console.log('test server running at port ',8123)
  10. })
  11. // app.use(bodyParser.raw())
  12. app.use(function(req,res,next){
  13. console.log(req.headers)
  14. next()
  15. })
  16. app.use(upload.any())
  17. app.use('/public', express.static('./public'))
  18. app.get('/redirect', function(req, res) {
  19. res.redirect('/public/github.png')
  20. })
  21. app.post('/upload', function(req, res){
  22. console.log(req.headers)
  23. console.log(req.body)
  24. fs.writeFile('./uploads/file'+Date.now()+'.png', req.body,function(err){
  25. if(!err)
  26. res.status(200).send({ message : 'ok'})
  27. else
  28. res.status(500).send({ message : err})
  29. })
  30. })
  31. app.post('/upload-form', function(req, res) {
  32. console.log(req.headers)
  33. console.log(req.body)
  34. console.log(req.files)
  35. if(Array.isArray(req.files)) {
  36. req.files.forEach((f) => {
  37. console.log(process.cwd() + f.path, '=>', process.cwd() + '/public/' + f.originalname)
  38. fs.renameSync('./' + f.path, './public/'+ f.originalname)
  39. })
  40. }
  41. res.status(200).send({
  42. fields : req.body,
  43. files : req.files
  44. })
  45. })