Няма описание

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