Açıklama Yok

server.js 4.5KB

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