first fully working version
This commit is contained in:
		
							parent
							
								
									40c668b6c5
								
							
						
					
					
						commit
						5fd846b174
					
				
							
								
								
									
										65
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										65
									
								
								index.js
									
									
									
									
									
								
							| @ -1,8 +1,10 @@ | |||||||
| const http = require('http'); | const http = require('http') | ||||||
| const url = require('url'); | const url = require('url') | ||||||
| const fs = require('fs'); | const fs = require('fs') | ||||||
| const ws = require('ws'); | const ws = require('ws') | ||||||
| const redis = require('redis') | const redis = require('redis') | ||||||
|  | const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest | ||||||
|  | const host = 'http://127.0.0.1:8080/' | ||||||
| 
 | 
 | ||||||
| //redis client
 | //redis client
 | ||||||
| const redis_cli = redis.createClient() | const redis_cli = redis.createClient() | ||||||
| @ -32,10 +34,16 @@ http.createServer(function (req, res) { | |||||||
|       return res.end(); |       return res.end(); | ||||||
|     }) |     }) | ||||||
|   }else{ |   }else{ | ||||||
|     let url = redis_cli.hget("surl;"+q.pathname.split("/", 2)[1], "url") |     redis_cli.hget("surl;"+q.pathname.split("/", 2)[1], "url", function(err, obj) { | ||||||
|     res.writeHead(302, {'Location': 'http://adb.sh'}); |       if(err) console.log(err); | ||||||
|     //res.write(data);
 |       if(obj){ | ||||||
|     return res.end(); |         res.writeHead(302, {'Location': obj}); | ||||||
|  |         return res.end(); | ||||||
|  |       }else{ | ||||||
|  |         res.writeHead(404, {'Content-Type': 'text/html'}); | ||||||
|  |         return res.end("404 this short-url does not exist :/"); | ||||||
|  |       } | ||||||
|  |     }); | ||||||
|   } |   } | ||||||
| }).listen(8080); | }).listen(8080); | ||||||
| 
 | 
 | ||||||
| @ -69,19 +77,38 @@ wss.on('connection', ws => { | |||||||
|     console.log(`Received message => ${message}`) |     console.log(`Received message => ${message}`) | ||||||
|     let msg = `${message}`.split(";", 2) |     let msg = `${message}`.split(";", 2) | ||||||
|     if (msg[0] === 'long_url') { |     if (msg[0] === 'long_url') { | ||||||
|       if (function valid_url(){ |       if (msg[1] === '') ws.send('error;url is empty') | ||||||
|         if (msg[1] === ''){ws.send('error;url is empty'); return false} |       else if (msg[1].length > 2000) ws.send('error;your url is too long') | ||||||
|         else if (msg[1].length > 2000){ws.send('error;your url is too long'); return false} |       else{ | ||||||
|         else return true |         let xhr = new XMLHttpRequest(); | ||||||
|       }() === true){ |         console.log(msg[1]) | ||||||
|         let ran_key = get_key(8) |         xhr.open('GET', msg[1], true); | ||||||
|         //redis_client.set("key", "value", redis.print)
 |         xhr.timeout = 2000; | ||||||
|         redis_cli.hmset("surl;"+ran_key, "url", msg[1], "time", Date.now()) |         xhr.ontimeout = function(e) {ws.send('error;url timed out')} | ||||||
|         ws.send('short_url;http://127.0.0.1/'+ran_key) |         xhr.onreadystatechange = function() { | ||||||
|  |           if (xhr.readyState === 4) { | ||||||
|  |             switch(xhr.status) { | ||||||
|  |               case 200: | ||||||
|  |                 let ran_key = get_key(8) | ||||||
|  |                 //write to redis
 | ||||||
|  |                 redis_cli.hmset("surl;"+ran_key, "url", msg[1], "time", Date.now()) | ||||||
|  |                 ws.send('surl;'+host+ran_key) | ||||||
|  |                 console.log('key;'+ran_key) | ||||||
|  |                 break; | ||||||
|  |               case 301: ws.send('error;the url is redirecting (301)');break; | ||||||
|  |               case 302: ws.send('error;the url is redirecting (302)');break; | ||||||
|  |               case 303: ws.send('error;the url is redirecting (303)');break; | ||||||
|  |               case 404: ws.send('error;site does not exist (404)');break; | ||||||
|  |               case 502: ws.send('error;remote server error (502)');break; | ||||||
|  |               case 500: ws.send('error;remote server error (500)');break; | ||||||
|  |               case 503: ws.send('error;remote server error (503)');break; | ||||||
|  |               default: ws.send('error;no valid url');break; | ||||||
|  |             } | ||||||
|  |           } | ||||||
|  |         } | ||||||
|  |         xhr.send(); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|     //ws.send('got your request: '+msg)
 |  | ||||||
|     console.log(msg[0]) |  | ||||||
|   }) |   }) | ||||||
|   ws.send('websocket connected') |   ws.send('websocket connected') | ||||||
| }) | }) | ||||||
|  | |||||||
| @ -3,6 +3,7 @@ | |||||||
|   "version": "1.0.0", |   "version": "1.0.0", | ||||||
|   "dependencies": { |   "dependencies": { | ||||||
|     "redis": "^3.0.2", |     "redis": "^3.0.2", | ||||||
|     "ws": "^7.3.1" |     "ws": "^7.3.1", | ||||||
|  |     "xmlhttprequest": "^1.8.0" | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -9,7 +9,7 @@ | |||||||
| 			const socket = new WebSocket(wsurl) | 			const socket = new WebSocket(wsurl) | ||||||
| 
 | 
 | ||||||
| 			socket.onopen = () => { | 			socket.onopen = () => { | ||||||
| 				socket.send('new_session ') | 				socket.send('new session') | ||||||
| 			} | 			} | ||||||
| 			socket.onerror = (error) => { | 			socket.onerror = (error) => { | ||||||
| 				console.log(`WebSocket error: ${error}`) | 				console.log(`WebSocket error: ${error}`) | ||||||
| @ -20,11 +20,18 @@ | |||||||
| 				if (msg[0] === 'error') { | 				if (msg[0] === 'error') { | ||||||
| 					document.getElementById('error-message').innerText = msg[1] | 					document.getElementById('error-message').innerText = msg[1] | ||||||
| 					document.getElementById('error-box').style.display = 'block' | 					document.getElementById('error-box').style.display = 'block' | ||||||
|  | 				} else if (msg[0] === 'surl') { | ||||||
|  | 					document.getElementById('surl-input').value = msg[1] | ||||||
|  | 					document.getElementById('new-user-box').style.display = 'block' | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 			function short_url(){ | 			function short_url(){ | ||||||
| 				socket.send('long_url '+document.getElementById('name-input').value) | 				socket.send('long_url '+document.getElementById('name-input').value) | ||||||
| 			} | 			} | ||||||
|  | 			function copy_url() { | ||||||
|  | 				document.getElementById('surl-input').select(); | ||||||
|  | 				document.execCommand("copy"); | ||||||
|  | 			} | ||||||
| 		</script> | 		</script> | ||||||
| 	</head> | 	</head> | ||||||
| 	<body> | 	<body> | ||||||
| @ -42,6 +49,22 @@ | |||||||
| 				</a> | 				</a> | ||||||
| 			</form> | 			</form> | ||||||
| 		</div> | 		</div> | ||||||
|  | 
 | ||||||
|  | 		<div class="box-dark" id="new-user-box"> | ||||||
|  | 			<div class="login"> | ||||||
|  | 					<div class="inputbox" id="surl"> | ||||||
|  | 						<input class="input" id="surl-input" type="text" onblur="deselected('surl');" onfocus="selected('surl');" size="30" value="" maxlength="20" placeholder="surl"> | ||||||
|  | 					</div> | ||||||
|  | 					<input type="hidden" value="search" name="login"> | ||||||
|  | 					<div class="btn-green" id="copy-btn" onclick="copy_url()"> | ||||||
|  | 						<div class="btn-text">copy</div> | ||||||
|  | 					</div> | ||||||
|  | 			</div> | ||||||
|  | 			<div onclick="visible('new-user-box','off')" style="position: absolute; top:5px; right: 5px;" class="sym_btn-invisible"> | ||||||
|  | 				<img class="icon" src="sym/ic_close_white_24px.svg" /> | ||||||
|  | 			</div> | ||||||
|  | 		</div> | ||||||
|  | 
 | ||||||
| 		<div id="error-box" class="error"> | 		<div id="error-box" class="error"> | ||||||
| 			<div onclick="document.getElementById('error-box').style.display = 'none'" style="position: absolute; top:5px; right: 5px;" class="sym_btn-invisible"> | 			<div onclick="document.getElementById('error-box').style.display = 'none'" style="position: absolute; top:5px; right: 5px;" class="sym_btn-invisible"> | ||||||
| 				<img class="icon" src="./sym/ic_close_white_24px.svg"> | 				<img class="icon" src="./sym/ic_close_white_24px.svg"> | ||||||
|  | |||||||
| @ -158,6 +158,12 @@ input[id="name-input"]::-webkit-input-placeholder { | |||||||
|     margin-left: calc(50% - 75px); |     margin-left: calc(50% - 75px); | ||||||
|     margin-top: 20px; |     margin-top: 20px; | ||||||
| } | } | ||||||
|  | #copy-btn { | ||||||
|  |     position: absolute; | ||||||
|  |     width: 150px; | ||||||
|  |     margin-left: calc(50% - 75px); | ||||||
|  |     margin-top: 20px; | ||||||
|  | } | ||||||
| #bottom-link-btn { | #bottom-link-btn { | ||||||
|     position: fixed; |     position: fixed; | ||||||
|     width: 150px; |     width: 150px; | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user