From 816286e197f21fbf89c5d186052a889cdb9fc0e6 Mon Sep 17 00:00:00 2001 From: adb-sh Date: Mon, 3 Aug 2020 13:50:58 +0200 Subject: [PATCH] backend file types --- index.js | 10 ++++++---- package.json | 1 + 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index de99553..38197c8 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ const url = require('url') const fs = require('fs') const ws = require('ws') const redis = require('redis') +const mime = require('mime') const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest const host = 'http://127.0.0.1:8080/' @@ -19,17 +20,18 @@ http.createServer(function (req, res) { const q = url.parse(req.url, true); let filename = "./public" + q.pathname; if (filename === "./public/") filename = "./public/index.html"; + let file_type = mime.getType(filename) //q.pathname.split("/", 2)[1] === "sym" || ) - if (function valid_path(){ + if (function valid_path(){// for (let i = 0; i < outpath.length; i++) if (q.pathname.split("/", 2)[1] === outpath[i]) return true return false }() === true){ fs.readFile(filename, function(err, data) { if (err) { - res.writeHead(404, {'Content-Type': 'text/html'}); + res.writeHead(404, {'Content-Type': file_type}); return res.end("404 Not Found"); } - res.writeHead(200, {'Content-Type': 'text/html'}); + res.writeHead(200, {'Content-Type': file_type}); res.write(data); return res.end(); }) @@ -40,7 +42,7 @@ http.createServer(function (req, res) { res.writeHead(302, {'Location': obj}); return res.end(); }else{ - res.writeHead(404, {'Content-Type': 'text/html'}); + res.writeHead(404, {'Content-Type': file_type}); return res.end("404 this short-url does not exist :/"); } }); diff --git a/package.json b/package.json index c7c5190..27d1e37 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "short_url", "version": "1.0.0", "dependencies": { + "mime": "^2.4.6", "redis": "^3.0.2", "ws": "^7.3.1", "xmlhttprequest": "^1.8.0"