No Description

server.js 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. LIB_SOURCE_PATH = '../src/',
  17. NODE_MODULE_MODULE_PATH = '../RNFetchBlobTest/node_modules/react-native-fetch-blob/',
  18. APP_SOURCE_PATH = '../RNFetchBlobTest/'
  19. // watch test app source
  20. watch(JS_SOURCE_PATH, APP_SOURCE_PATH)
  21. // watch lib js source
  22. watch(LIB_SOURCE_PATH, NODE_MODULE_MODULE_PATH, {ignored: /\.\.\/src\/(android|ios)\//})
  23. app.listen(8123, function(err){
  24. if(!err)
  25. console.log('test server running at port ',8123)
  26. })
  27. app.use(function(req,res,next){
  28. console.log(chalk.green('request url=') + chalk.magenta(req.url))
  29. next()
  30. })
  31. app.use(upload.any())
  32. app.use('/public', express.static('./public'))
  33. // for redirect test
  34. app.get('/redirect', function(req, res) {
  35. res.redirect('/public/github.png')
  36. })
  37. // return an empty response
  38. app.all('/empty', function(req, res) {
  39. res.send('')
  40. })
  41. // handle multipart/form-data request
  42. app.post('/upload-form', function(req, res) {
  43. console.log(req.headers)
  44. console.log(req.body)
  45. console.log(req.files)
  46. if(Array.isArray(req.files)) {
  47. req.files.forEach((f) => {
  48. console.log(process.cwd() + f.path, '=>', process.cwd() + '/public/' + f.originalname)
  49. fs.renameSync('./' + f.path, './public/'+ f.originalname)
  50. })
  51. }
  52. res.status(200).send({
  53. fields : req.body,
  54. files : req.files
  55. })
  56. })
  57. function watch(source, dest, ignore) {
  58. // watch files in test folder
  59. chokidar
  60. .watch(source, ignore)
  61. .on('add', function(path) {
  62. console.log(chalk.magenta('file created'), path)
  63. var targetPath = String(path).replace(source, dest)
  64. mkdirp(dirname(targetPath), function (err) {
  65. if (err) return cb(err)
  66. fs.writeFileSync(targetPath, fs.readFileSync(path))
  67. })
  68. })
  69. .on('change', function(path) {
  70. console.log(chalk.green('file changed'), path)
  71. var targetPath = String(path).replace(source, dest)
  72. mkdirp(dirname(targetPath), function (err) {
  73. if (err) return cb(err)
  74. fs.writeFileSync(targetPath, fs.readFileSync(path))
  75. })
  76. })
  77. .on('unlink', function(path) {
  78. console.log(chalk.red('file removed'), path)
  79. var targetPath = String(path).replace(source, dest)
  80. mkdirp(dirname(targetPath), function (err) {
  81. if (err) return cb(err)
  82. fs.unlinkSync(targetPath)
  83. })
  84. })
  85. .on('error', function(err){
  86. console.log(err)
  87. })
  88. }