Last active
August 4, 2021 03:21
-
-
Save gatherKnowledge/e83f884db92a12c73cfbd4d2a23278b9 to your computer and use it in GitHub Desktop.
This file contains 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 readline = require("readline"); | |
const chalk = require("chalk"); | |
const axios = require("axios"); | |
const PROTOCOL = "http"; | |
const URI = "localhost"; | |
const PORT = 3000; | |
const SERVER = `${PROTOCOL}://${URI}:${PORT}`; | |
const PREFIX_PATH = "produce"; | |
const TOKEN = "xxxxxxxxxxx"; | |
const axiosInstance = axios.create({ | |
baseURL: `${SERVER}`, | |
headers: { Authorization: `Bearer ${TOKEN}` }, | |
timeout: 3000, | |
}); | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
}); | |
console.log(chalk.blue("BARCODE READER DEVICE TESTER")); | |
let dots = ""; | |
const interval = setInterval(() => { | |
console.log(chalk.blue(`RUNNING${dots}`)); | |
dots += "."; | |
if (dots.length === 10) { | |
clearInterval(interval); | |
console.clear(); | |
rl.question("수량을 입력해주세요. ", (quantity) => { | |
const qty = parseInt(quantity); | |
console.clear(); | |
if (isNaN(qty)) throw new Error("숫자만 입력가능합니다."); | |
console.log(chalk.blue(`입력 수량: ${qty}`)); | |
console.log("준비 완료"); | |
rl.on("line", async function (line) { | |
try { | |
const response = await axiosInstance.post(`/${PREFIX_PATH}`, { | |
itemCode: line, | |
qty, | |
}); | |
const { status, data } = response; | |
if (status === 200 && data === "ok") { | |
console.log("추가 완료"); | |
} else { | |
console.log("오류가 발생했습니다."); | |
} | |
} catch (e) { | |
console.error(e); | |
} | |
}).on("close", function () { | |
process.exit(); | |
}); | |
}); | |
} | |
}, 100); |
This file contains 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
{ | |
"name": "plc-sample", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "./node_modules/.bin/ts-node index.ts" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"axios": "^0.21.1", | |
"chalk": "^4.1.2", | |
"ts-node": "^10.1.0", | |
"typescript": "^4.3.5" | |
}, | |
"devDependencies": { | |
"@types/node": "^16.4.10", | |
"tslib": "^2.3.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment