From 39c632eda398b5294186b5a3b1d71e9e575f4485 Mon Sep 17 00:00:00 2001 From: kaotisk Date: Mon, 14 Oct 2024 04:44:28 +0300 Subject: Clean up and refactoring --- server/index.js | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) (limited to 'server') diff --git a/server/index.js b/server/index.js index 589d7db..64b4138 100755 --- a/server/index.js +++ b/server/index.js @@ -1,15 +1,6 @@ const http = require("node:http"); const fs = require("node:fs"); -// const welcomeMessage = require("./routes/default/index.js"); -// const getNodeInfo = require('./routes/getNodeInfo/index.js'); -// const getPeers = require('./routes/getPeers/index.js'); -// const getZblock = require('./routes/getZblock/index.js'); -// const getZlatest = require('./routes/getZLatest/index.js'); -// const getSblock = require('./routes/getSBlock/index.js'); -// const getSlatest = require('./routes/getSLatest/index.js'); - -// const akLogMessage = require('./lib/akLogMessage'); function akLogMessage(type, message) { console.log(type+": "+message); @@ -36,12 +27,17 @@ function respondError(res, errorMessage) function respondJSON(res, hash) { test = /[0-9a-f]{128}/ - if (test.test(hash)) + if(test.test(hash)) { - res.writeHead(200, { 'Content-Type': 'application/json'}); - res.end(JSON.stringify(JSON.parse(fs.readFileSync('../hashes/' + hash)))); - } else { - respondError(res, 'Not hash'); + var filename = '../hashes/' + hash; + var stat = fs.statSync(filename); + res.writeHead(200, { 'Content-Type': 'application/json', 'Content-Length': stat.size}); + var readStream = fs.createReadStream(filename); + readStream.pipe(res); + } + else + { + respondError(res, 'Not found'); } } @@ -72,10 +68,20 @@ function applicationRoutes(req, res, args) // Audio returns function respondOGG(res, hash) { - res.writeHead(200, { 'Content-Type': 'audio/ogg'}); test = /[0-9a-f]{128}/ - console.log(test.test(hash)) - res.end(fs.readFileSync('../hashes/' + hash)); + if(test.test(hash)) + { + var filename = '../hashes/' + hash; + var stat = fs.statSync(filename); + res.writeHead(200, { 'Content-Type': 'audio/ogg', 'Content-Length': stat.size}); + var readStream = fs.createReadStream(filename); + readStream.pipe(res); + } + else + { + respondError(res, 'Not found'); + } + } function audioRoutes(req, res, args) -- cgit v1.2.3