설명 없음

server.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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.get('/10s-download', (req,res) => {
  141. var count = 0
  142. var data = ''
  143. for(var i =0;i<1024000;i++)
  144. data += '1'
  145. res.set('Contet-Length', 1024000*10)
  146. var it = setInterval(() => {
  147. res.write(data)
  148. count++
  149. if(count == 10) {
  150. clearInterval(it)
  151. res.end()
  152. }
  153. }, 1000)
  154. })
  155. app.all('/long', (req, res) => {
  156. var count = 0;
  157. var it = setInterval(() => {
  158. console.log('write data', count)
  159. res.write('a')
  160. if(++count >60){
  161. clearInterval(it)
  162. res.end()
  163. }
  164. }, 1000);
  165. })
  166. app.all('/timeout', (req, res) => {
  167. })
  168. function formUpload(req, res) {
  169. console.log(req.headers)
  170. console.log(req.body)
  171. console.log(req.files)
  172. if(Array.isArray(req.files)) {
  173. req.files.forEach((f) => {
  174. console.log(process.cwd() + f.path, '=>', process.cwd() + '/public/' + f.originalname)
  175. fs.renameSync('./' + f.path, './public/'+ f.originalname)
  176. })
  177. }
  178. res.status(200).send({
  179. fields : req.body,
  180. files : req.files
  181. })
  182. }
  183. function watch(source, dest, ignore) {
  184. // watch files in test folder
  185. chokidar
  186. .watch(source, ignore)
  187. .on('add', function(path) {
  188. console.log(chalk.magenta('file created'), path)
  189. var targetPath = String(path).replace(source, dest)
  190. mkdirp(dirname(targetPath), function (err) {
  191. if (err) return cb(err)
  192. fs.writeFileSync(targetPath, fs.readFileSync(path))
  193. })
  194. })
  195. .on('change', function(path) {
  196. console.log(chalk.green('file changed'), path)
  197. var targetPath = String(path).replace(source, dest)
  198. mkdirp(dirname(targetPath), function (err) {
  199. if (err) return cb(err)
  200. fs.writeFileSync(targetPath, fs.readFileSync(path))
  201. })
  202. })
  203. .on('unlink', function(path) {
  204. console.log(chalk.red('file removed'), path)
  205. var targetPath = String(path).replace(source, dest)
  206. mkdirp(dirname(targetPath), function (err) {
  207. if (err) return cb(err)
  208. fs.unlinkSync(targetPath)
  209. })
  210. })
  211. .on('error', function(err){
  212. console.log(err)
  213. })
  214. }