暂无描述

server.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. // handle octet-stream request
  38. app.post('/upload', function(req, res){
  39. console.log(req.headers)
  40. console.log(req.body)
  41. fs.writeFile('./uploads/file'+Date.now()+'.png', req.body,function(err){
  42. if(!err)
  43. res.status(200).send({ message : 'ok'})
  44. else
  45. res.status(500).send({ message : err})
  46. })
  47. })
  48. // return an empty response
  49. app.all('/empty', function(req, res) {
  50. res.send('')
  51. })
  52. // handle multipart/form-data request
  53. app.post('/upload-form', function(req, res) {
  54. console.log(req.headers)
  55. console.log(req.body)
  56. console.log(req.files)
  57. if(Array.isArray(req.files)) {
  58. req.files.forEach((f) => {
  59. console.log(process.cwd() + f.path, '=>', process.cwd() + '/public/' + f.originalname)
  60. fs.renameSync('./' + f.path, './public/'+ f.originalname)
  61. })
  62. }
  63. res.status(200).send({
  64. fields : req.body,
  65. files : req.files
  66. })
  67. })
  68. function watch(source, dest, ignore) {
  69. // watch files in test folder
  70. chokidar
  71. .watch(source, ignore)
  72. .on('add', function(path) {
  73. console.log(chalk.magenta('file created'), path)
  74. var targetPath = String(path).replace(source, dest)
  75. mkdirp(dirname(targetPath), function (err) {
  76. if (err) return cb(err)
  77. fs.writeFileSync(targetPath, fs.readFileSync(path))
  78. })
  79. })
  80. .on('change', function(path) {
  81. console.log(chalk.green('file changed'), path)
  82. var targetPath = String(path).replace(source, dest)
  83. mkdirp(dirname(targetPath), function (err) {
  84. if (err) return cb(err)
  85. fs.writeFileSync(targetPath, fs.readFileSync(path))
  86. })
  87. })
  88. .on('unlink', function(path) {
  89. console.log(chalk.red('file removed'), path)
  90. var targetPath = String(path).replace(source, dest)
  91. mkdirp(dirname(targetPath), function (err) {
  92. if (err) return cb(err)
  93. fs.unlinkSync(targetPath)
  94. })
  95. })
  96. .on('error', function(err){
  97. console.log(err)
  98. })
  99. }