Created
May 7, 2020 08:07
-
-
Save Restry/4d183e0c67e1e06c0e11a661828878b3 to your computer and use it in GitHub Desktop.
[递归获取本地目录文件] 支持上传到指定服务器 #nodejs #js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fs = require('fs'); | |
| const path = require('path'); | |
| const fetch = require('node-fetch') | |
| const model = { | |
| "mode": | |
| "{\n \"data\": {}\n}", | |
| "url": "/test", | |
| "method": "get", | |
| "description": "description", | |
| "project_id": "5e9c5820e8f1820020a10de6" | |
| } | |
| class FileItem { | |
| constructor(fileName, filePath, parent) { | |
| this.description = fileName; | |
| // this.path = filePath; | |
| this.url = path.join(parent, fileName).replace('.json', '').replace('api/', '/'); | |
| this.mode = fs.readFileSync(filePath).toLocaleString(); | |
| } | |
| mode = ''; | |
| url = ''; | |
| method = 'get'; | |
| description = ''; | |
| project_id = '5e9c5820e8f1820020a10de6'; | |
| } | |
| const getDirFiles = (dir) => fs.readdirSync(dir) //可以根据项目情况,自动分析目录文件生成 | |
| .map(a => new FileItem(a, dir)); | |
| const arr2Obj = (arr = [], name, value) => { | |
| const obj = {}; | |
| arr.forEach(a => obj[a[name]] = a[value]); | |
| return obj; | |
| } | |
| const getEntrys = (entryPath) => fs.readdirSync(entryPath).map(entry => { | |
| const itemPath = path.join(entryPath, entry); | |
| const viewStat = fs.statSync(itemPath); | |
| const isDir = viewStat.isDirectory(); | |
| if (!isDir) return new FileItem(entry, itemPath, entryPath); | |
| return getEntrys(itemPath) | |
| });//.filter(a => a.isDir); | |
| function flatten(arr) { | |
| return arr.reduce((result, item) => { | |
| return result.concat(Array.isArray(item) ? flatten(item) : item); | |
| }, []); | |
| } | |
| const files = flatten(getEntrys('api')); | |
| console.log(files.length); | |
| files.forEach((file) => { | |
| console.info(file); | |
| fetch("https://windart-api.chinanorth.cloudapp.chinacloudapi.cn/api/mock/create", { | |
| "headers": { | |
| "accept": "application/json, text/plain, */*", | |
| "accept-language": "zh-CN,zh;q=0.9", | |
| "authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZ GExMGRlNSIsImlhdCI6MTU4NzgxMTIzMywiZXhwIjoxNTg5MDIwODMzfQ.qaoYG_Ht8ZBV9xt-0QNQ_D2Q8f1StCnMOF42YVDnIxw", | |
| "content-type": "application/json;charset=UTF-8", | |
| "sec-fetch-dest": "empty", | |
| "sec-fetch-mode": "cors", | |
| "sec-fetch-site": "same-origin", | |
| "cookie": "easy-mock_token=eyJhbGciOiJIUzI1NiIsInR 1ODIwZThmMTgyMDAyMGExMGRlNSIsImlhdCI6MTU4NzgxMTIzMywiZXhwIjoxNTg5MDIwODMzfQ.qaoYG_Ht8ZBV9xt-0QNQ_D2Q8f1StCnMOF42YVDnIxw" | |
| }, | |
| "referrer": "https://windart-api.chinanorth.cloudapp.chinacloudapi.cn/project/5e9c5820e8f1820020a10de6", | |
| "referrerPolicy": "no-referrer-when-downgrade", | |
| "body": JSON.stringify(file), | |
| "method": "POST", | |
| "mode": "cors" | |
| }).then(res => { | |
| console.log(JSON.stringify(res)); | |
| }).catch(err => { | |
| console.error(err.message); | |
| }); | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment