backend file types
This commit is contained in:
parent
5fd846b174
commit
816286e197
10
index.js
10
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 :/");
|
||||
}
|
||||
});
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user