説明なし

server.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 https = require('https')
  16. var JS_SOURCE_PATH = '../test/',
  17. LIB_SOURCE_PATH = '../src/',
  18. NODE_MODULE_MODULE_PATH = '../RNFetchBlobTest/node_modules/react-native-fetch-blob/',
  19. APP_SOURCE_PATH = '../RNFetchBlobTest/'
  20. // watch test app source
  21. watch(JS_SOURCE_PATH, APP_SOURCE_PATH)
  22. // watch lib js source
  23. watch(LIB_SOURCE_PATH, NODE_MODULE_MODULE_PATH, {ignored: /\.\.\/src\/(android|ios)\//})
  24. // https
  25. var server = https.createServer({
  26. key : fs.readFileSync('./key.pem'),
  27. cert : fs.readFileSync('./cert.pem')
  28. }, app).listen(8124, function(err){
  29. if(!err)
  30. console.log('SSL test server running at port ',8124)
  31. })
  32. // http
  33. app.listen(8123, function(err){
  34. if(!err)
  35. console.log('test server running at port ',8123)
  36. })
  37. var count = 0
  38. app.use(function(req,res,next){
  39. console.log(chalk.green('request url=') + chalk.magenta(req.url), ++count)
  40. next()
  41. })
  42. app.use('/upload-form', function(req, res, next) {
  43. console.log(req.headers)
  44. // req.on('data', (chunk) => {
  45. // console.log(String(chunk,'utf8'))
  46. // })
  47. // req.on('end', () => {
  48. next()
  49. // })
  50. })
  51. app.use(upload.any())
  52. app.use('/public', express.static('./public'))
  53. // for redirect test
  54. app.get('/redirect', function(req, res) {
  55. res.redirect('/public/github.png')
  56. })
  57. app.all('/params', function(req, res) {
  58. console.log(req.url)
  59. var resp =
  60. {
  61. time : req.query.time,
  62. name : req.query.name,
  63. lang : req.query.lang
  64. }
  65. console.log(resp)
  66. res.send(resp)
  67. })
  68. // return an empty response
  69. app.all('/empty', function(req, res) {
  70. res.send('')
  71. })
  72. app.delete('/hey', function(req, res) {
  73. res.send('man')
  74. })
  75. app.post('/mime', mimeCheck)
  76. app.put('/mime', mimeCheck)
  77. function mimeCheck(req, res) {
  78. console.log(req.files)
  79. var mimes = []
  80. for(var i in req.files) {
  81. mimes.push(req.files[i].mimetype)
  82. }
  83. res.send(mimes)
  84. }
  85. // handle multipart/form-data request
  86. app.post('/upload-form', formUpload)
  87. app.put('/upload-form', formUpload)
  88. function formUpload(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. }
  103. function watch(source, dest, ignore) {
  104. // watch files in test folder
  105. chokidar
  106. .watch(source, ignore)
  107. .on('add', function(path) {
  108. console.log(chalk.magenta('file created'), path)
  109. var targetPath = String(path).replace(source, dest)
  110. mkdirp(dirname(targetPath), function (err) {
  111. if (err) return cb(err)
  112. fs.writeFileSync(targetPath, fs.readFileSync(path))
  113. })
  114. })
  115. .on('change', function(path) {
  116. console.log(chalk.green('file changed'), path)
  117. var targetPath = String(path).replace(source, dest)
  118. mkdirp(dirname(targetPath), function (err) {
  119. if (err) return cb(err)
  120. fs.writeFileSync(targetPath, fs.readFileSync(path))
  121. })
  122. })
  123. .on('unlink', function(path) {
  124. console.log(chalk.red('file removed'), path)
  125. var targetPath = String(path).replace(source, dest)
  126. mkdirp(dirname(targetPath), function (err) {
  127. if (err) return cb(err)
  128. fs.unlinkSync(targetPath)
  129. })
  130. })
  131. .on('error', function(err){
  132. console.log(err)
  133. })
  134. }