Keine Beschreibung

server.js 3.1KB

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