Ingen beskrivning

server.js 5.2KB

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