説明なし

server.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.use(upload.any())
  56. app.use('/public', express.static('./public'))
  57. // for redirect test
  58. app.get('/redirect', function(req, res) {
  59. res.redirect('/public/github.png')
  60. })
  61. app.all('/params', function(req, res) {
  62. console.log(req.url)
  63. var resp =
  64. {
  65. time : req.query.time,
  66. name : req.query.name,
  67. lang : req.query.lang
  68. }
  69. console.log(resp)
  70. res.send(resp)
  71. })
  72. // return an empty response
  73. app.all('/empty', function(req, res) {
  74. res.send('')
  75. })
  76. app.delete('/hey', function(req, res) {
  77. res.send('man')
  78. })
  79. app.post('/mime', mimeCheck)
  80. app.put('/mime', mimeCheck)
  81. function mimeCheck(req, res) {
  82. console.log(req.files)
  83. var mimes = []
  84. for(var i in req.files) {
  85. mimes.push(req.files[i].mimetype)
  86. }
  87. res.send(mimes)
  88. }
  89. // handle multipart/form-data request
  90. app.post('/upload-form', formUpload)
  91. app.put('/upload-form', formUpload)
  92. // for XHR tests
  93. //
  94. app.all('/xhr-code/:code', (req, res) => {
  95. console.log('code = ', req.params.code)
  96. res.status(Math.floor(req.params.code)).send()
  97. })
  98. app.all('/xhr-header', (req, res) => {
  99. console.log(req.headers)
  100. res.send(req.headers)
  101. })
  102. app.post('/upload', bodyParser.urlencoded({ extended: true }), (req, res) => {
  103. console.log(JSON.stringify(req.headers))
  104. console.log(JSON.stringify(req.body))
  105. res.status(200).send(req.body)
  106. })
  107. app.all('/timeout', (res, req) => {
  108. })
  109. function formUpload(req, res) {
  110. console.log(req.headers)
  111. console.log(req.body)
  112. console.log(req.files)
  113. if(Array.isArray(req.files)) {
  114. req.files.forEach((f) => {
  115. console.log(process.cwd() + f.path, '=>', process.cwd() + '/public/' + f.originalname)
  116. fs.renameSync('./' + f.path, './public/'+ f.originalname)
  117. })
  118. }
  119. res.status(200).send({
  120. fields : req.body,
  121. files : req.files
  122. })
  123. }
  124. function watch(source, dest, ignore) {
  125. // watch files in test folder
  126. chokidar
  127. .watch(source, ignore)
  128. .on('add', function(path) {
  129. console.log(chalk.magenta('file created'), path)
  130. var targetPath = String(path).replace(source, dest)
  131. mkdirp(dirname(targetPath), function (err) {
  132. if (err) return cb(err)
  133. fs.writeFileSync(targetPath, fs.readFileSync(path))
  134. })
  135. })
  136. .on('change', function(path) {
  137. console.log(chalk.green('file changed'), path)
  138. var targetPath = String(path).replace(source, dest)
  139. mkdirp(dirname(targetPath), function (err) {
  140. if (err) return cb(err)
  141. fs.writeFileSync(targetPath, fs.readFileSync(path))
  142. })
  143. })
  144. .on('unlink', function(path) {
  145. console.log(chalk.red('file removed'), path)
  146. var targetPath = String(path).replace(source, dest)
  147. mkdirp(dirname(targetPath), function (err) {
  148. if (err) return cb(err)
  149. fs.unlinkSync(targetPath)
  150. })
  151. })
  152. .on('error', function(err){
  153. console.log(err)
  154. })
  155. }