Нет описания

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. app.use(function(req,res,next){
  38. console.log(chalk.green('request url=') + chalk.magenta(req.url))
  39. next()
  40. })
  41. app.use('/upload-form', function(req, res, next) {
  42. console.log(req.headers)
  43. // req.on('data', (chunk) => {
  44. // console.log(String(chunk,'utf8'))
  45. // })
  46. // req.on('end', () => {
  47. next()
  48. // })
  49. })
  50. var count = 0
  51. app.use(function(req, res, next) {
  52. console.log(req.url, ++count);
  53. next();
  54. })
  55. app.get('/unicode', (req, res) => {
  56. res.send({ data:'你好!'})
  57. })
  58. app.all('/echo', (req, res) => {
  59. var body = ''
  60. req.on('data', (chunk) => {
  61. body+=chunk
  62. })
  63. req.on('end', () => {
  64. res.send({
  65. headers : req.headers,
  66. body : body
  67. })
  68. })
  69. })
  70. app.use(upload.any())
  71. app.use('/public', express.static('./public'))
  72. // for redirect test
  73. app.get('/redirect', function(req, res) {
  74. res.redirect('/public/github.png')
  75. })
  76. app.all('/params', function(req, res) {
  77. console.log(req.url)
  78. var resp =
  79. {
  80. time : req.query.time,
  81. name : req.query.name,
  82. lang : req.query.lang
  83. }
  84. console.log(resp)
  85. res.send(resp)
  86. })
  87. // return an empty response
  88. app.all('/empty', function(req, res) {
  89. res.send('')
  90. })
  91. app.delete('/hey', function(req, res) {
  92. res.send('man')
  93. })
  94. app.get('/stress/:id', function(req, res) {
  95. res.sendFile(process.cwd() + '/public/github.png')
  96. })
  97. app.post('/mime', mimeCheck)
  98. app.put('/mime', mimeCheck)
  99. function mimeCheck(req, res) {
  100. console.log(req.files)
  101. var mimes = []
  102. for(var i in req.files) {
  103. mimes.push(req.files[i].mimetype)
  104. }
  105. res.send(mimes)
  106. }
  107. // handle multipart/form-data request
  108. app.post('/upload-form', formUpload)
  109. app.put('/upload-form', formUpload)
  110. // for XHR tests
  111. //
  112. app.all('/xhr-code/:code', (req, res) => {
  113. console.log('code = ', req.params.code)
  114. res.status(Math.floor(req.params.code)).send()
  115. })
  116. app.all('/content-length', (req, res) => {
  117. console.log(req.headers)
  118. res.send(req.headers['Content-Length'])
  119. })
  120. app.all('/xhr-header', (req, res) => {
  121. console.log(req.headers)
  122. // res.header('Content-Type', 'application/json')
  123. res.send(req.headers)
  124. })
  125. app.post('/upload', bodyParser.urlencoded({ extended : true }), (req, res) => {
  126. console.log(JSON.stringify(req.headers))
  127. console.log(JSON.stringify(req.body))
  128. res.status(200).send(req.body)
  129. })
  130. app.all('/timeout408/:time', (req, res) => {
  131. setTimeout(function() {
  132. res.status(408).send('request timed out.')
  133. }, 5000)
  134. })
  135. app.all('/long', (req, res) => {
  136. var count = 0;
  137. var it = setInterval(() => {
  138. console.log('write data', count)
  139. res.write('a')
  140. if(++count >60){
  141. clearInterval(it)
  142. res.end()
  143. }
  144. }, 1000);
  145. })
  146. app.all('/timeout', (req, res) => {
  147. })
  148. function formUpload(req, res) {
  149. console.log(req.headers)
  150. console.log(req.body)
  151. console.log(req.files)
  152. if(Array.isArray(req.files)) {
  153. req.files.forEach((f) => {
  154. console.log(process.cwd() + f.path, '=>', process.cwd() + '/public/' + f.originalname)
  155. fs.renameSync('./' + f.path, './public/'+ f.originalname)
  156. })
  157. }
  158. res.status(200).send({
  159. fields : req.body,
  160. files : req.files
  161. })
  162. }
  163. function watch(source, dest, ignore) {
  164. // watch files in test folder
  165. chokidar
  166. .watch(source, ignore)
  167. .on('add', function(path) {
  168. console.log(chalk.magenta('file created'), path)
  169. var targetPath = String(path).replace(source, dest)
  170. mkdirp(dirname(targetPath), function (err) {
  171. if (err) return cb(err)
  172. fs.writeFileSync(targetPath, fs.readFileSync(path))
  173. })
  174. })
  175. .on('change', function(path) {
  176. console.log(chalk.green('file changed'), path)
  177. var targetPath = String(path).replace(source, dest)
  178. mkdirp(dirname(targetPath), function (err) {
  179. if (err) return cb(err)
  180. fs.writeFileSync(targetPath, fs.readFileSync(path))
  181. })
  182. })
  183. .on('unlink', function(path) {
  184. console.log(chalk.red('file removed'), path)
  185. var targetPath = String(path).replace(source, dest)
  186. mkdirp(dirname(targetPath), function (err) {
  187. if (err) return cb(err)
  188. fs.unlinkSync(targetPath)
  189. })
  190. })
  191. .on('error', function(err){
  192. console.log(err)
  193. })
  194. }