Sin descripción

server.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. var express = require('express');
  2. var bodyParser = require('body-parser');
  3. var chokidar = require('chokidar');
  4. var multer = require('multer');
  5. var upload = multer({dest : 'uploads/'});
  6. var chalk = require('chalk');
  7. var mkdirp = require('mkdirp');
  8. var dirname = require('path').dirname;
  9. var app = express();
  10. var fs = require('fs');
  11. var JS_SOURCE_PATH = '../test/',
  12. APP_SOURCE_PATH = '../RNFetchBlobTest/';
  13. chokidar
  14. .watch('../src/index.js')
  15. .on('change', function(path) {
  16. console.log(chalk.green('js file changed'), path);
  17. var targetPath = String(path).replace('../src/', '../RNFetchBlobTest/node_modules/react-native-fetch-blob/')
  18. mkdirp(dirname(targetPath), function (err) {
  19. if (err) return cb(err);
  20. fs.writeFileSync(targetPath, fs.readFileSync(path));
  21. });
  22. })
  23. chokidar
  24. .watch(JS_SOURCE_PATH)
  25. .on('add', function(path) {
  26. console.log(chalk.magenta('file created'), path);
  27. var targetPath = String(path).replace(JS_SOURCE_PATH, APP_SOURCE_PATH)
  28. mkdirp(dirname(targetPath), function (err) {
  29. if (err) return cb(err);
  30. fs.writeFileSync(targetPath, fs.readFileSync(path));
  31. });
  32. })
  33. .on('change', function(path) {
  34. console.log(chalk.green('file changed'), path);
  35. var targetPath = String(path).replace(JS_SOURCE_PATH, APP_SOURCE_PATH)
  36. mkdirp(dirname(targetPath), function (err) {
  37. if (err) return cb(err);
  38. fs.writeFileSync(targetPath, fs.readFileSync(path));
  39. });
  40. })
  41. .on('unlink', function(path) {
  42. console.log(chalk.red('file removed'), path);
  43. var targetPath = String(path).replace(JS_SOURCE_PATH, APP_SOURCE_PATH)
  44. mkdirp(dirname(targetPath), function (err) {
  45. if (err) return cb(err);
  46. fs.unlinkSync(targetPath);
  47. });
  48. })
  49. .on('error', function(err){
  50. console.log(err);
  51. });
  52. app.listen(8123, function(err){
  53. if(!err)
  54. console.log('test server running at port ',8123)
  55. })
  56. app.use(function(req,res,next){
  57. console.log(req.headers)
  58. next()
  59. })
  60. app.use(upload.any())
  61. app.use('/public', express.static('./public'))
  62. app.get('/redirect', function(req, res) {
  63. res.redirect('/public/github.png')
  64. })
  65. app.post('/upload', function(req, res){
  66. console.log(req.headers)
  67. console.log(req.body)
  68. fs.writeFile('./uploads/file'+Date.now()+'.png', req.body,function(err){
  69. if(!err)
  70. res.status(200).send({ message : 'ok'})
  71. else
  72. res.status(500).send({ message : err})
  73. })
  74. })
  75. app.post('/upload-form', function(req, res) {
  76. console.log(req.headers)
  77. console.log(req.body)
  78. console.log(req.files)
  79. if(Array.isArray(req.files)) {
  80. req.files.forEach((f) => {
  81. console.log(process.cwd() + f.path, '=>', process.cwd() + '/public/' + f.originalname)
  82. fs.renameSync('./' + f.path, './public/'+ f.originalname)
  83. })
  84. }
  85. res.status(200).send({
  86. fields : req.body,
  87. files : req.files
  88. })
  89. })