No Description

server.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. var express = require('express');
  2. var bodyParser = require('body-parser');
  3. var chokidar = require('chokidar');
  4. var multer = require('multer');
  5. var upload = multer({dest : 'uploads/'});
  6. var chalk = require('chalk');
  7. var mkdirp = require('mkdirp');
  8. var dirname = require('path').dirname;
  9. var app = express();
  10. var fs = require('fs');
  11. var JS_SOURCE_PATH = '../test/',
  12. APP_SOURCE_PATH = '../RNFetchBlobTest/';
  13. // watch index.js in src folder
  14. chokidar
  15. .watch('../src/index.js')
  16. .on('change', function(path) {
  17. console.log(chalk.green('js file changed'), path);
  18. var targetPath = String(path).replace('../src/', '../RNFetchBlobTest/node_modules/react-native-fetch-blob/')
  19. mkdirp(dirname(targetPath), function (err) {
  20. if (err) return cb(err);
  21. fs.writeFileSync(targetPath, fs.readFileSync(path));
  22. });
  23. })
  24. // watch files in test folder
  25. chokidar
  26. .watch(JS_SOURCE_PATH)
  27. .on('add', function(path) {
  28. console.log(chalk.magenta('file created'), path);
  29. var targetPath = String(path).replace(JS_SOURCE_PATH, APP_SOURCE_PATH)
  30. mkdirp(dirname(targetPath), function (err) {
  31. if (err) return cb(err);
  32. fs.writeFileSync(targetPath, fs.readFileSync(path));
  33. });
  34. })
  35. .on('change', function(path) {
  36. console.log(chalk.green('file changed'), path);
  37. var targetPath = String(path).replace(JS_SOURCE_PATH, APP_SOURCE_PATH)
  38. mkdirp(dirname(targetPath), function (err) {
  39. if (err) return cb(err);
  40. fs.writeFileSync(targetPath, fs.readFileSync(path));
  41. });
  42. })
  43. .on('unlink', function(path) {
  44. console.log(chalk.red('file removed'), path);
  45. var targetPath = String(path).replace(JS_SOURCE_PATH, APP_SOURCE_PATH)
  46. mkdirp(dirname(targetPath), function (err) {
  47. if (err) return cb(err);
  48. fs.unlinkSync(targetPath);
  49. });
  50. })
  51. .on('error', function(err){
  52. console.log(err);
  53. });
  54. app.listen(8123, function(err){
  55. if(!err)
  56. console.log('test server running at port ',8123)
  57. })
  58. app.use(function(req,res,next){
  59. console.log(req.headers)
  60. next()
  61. })
  62. app.use(upload.any())
  63. app.use('/public', express.static('./public'))
  64. app.get('/redirect', function(req, res) {
  65. res.redirect('/public/github.png')
  66. })
  67. app.post('/upload', function(req, res){
  68. console.log(req.headers)
  69. console.log(req.body)
  70. fs.writeFile('./uploads/file'+Date.now()+'.png', req.body,function(err){
  71. if(!err)
  72. res.status(200).send({ message : 'ok'})
  73. else
  74. res.status(500).send({ message : err})
  75. })
  76. })
  77. app.post('/upload-form', function(req, res) {
  78. console.log(req.headers)
  79. console.log(req.body)
  80. console.log(req.files)
  81. if(Array.isArray(req.files)) {
  82. req.files.forEach((f) => {
  83. console.log(process.cwd() + f.path, '=>', process.cwd() + '/public/' + f.originalname)
  84. fs.renameSync('./' + f.path, './public/'+ f.originalname)
  85. })
  86. }
  87. res.status(200).send({
  88. fields : req.body,
  89. files : req.files
  90. })
  91. })