Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var http = require('http');
- var url = require('url');
- //var fs = require('fs');
- var Storage = require('node-storage');
- var login = new Storage("~/login");
- var server = http.createServer(function (request, response) {
- response.writeHead(200, {'Content-Type': 'text/plain'});
- response.write('Metodo: '+request.method+'\n');
- response.write('URL: '+request.url+'\n');
- var parse = url.parse(request.url, true);
- var path = parse.pathname;
- switch(request.method) {
- case "GET":
- response.writeHead(200, {
- 'Content-Type': 'text/event-stream',
- 'Cache-Control': 'no-cache',
- 'Access-Control-Allow-Origin': '*',
- 'Connection': 'keep-alive'
- });
- DoPost(request, response, path);
- break;
- case "POST":
- response.writeHead(200, {
- 'Content-Type': 'application/javascript',
- 'Cache-Control': 'no-cache',
- 'Access-Control-Allow-Origin': '*'
- });
- DoGet(request, response, path);
- break;
- default:
- response.writeHead(501);
- response.end();
- }
- });
- function DoPost(request, response, path) {
- switch(path) {
- case "/register":
- register(request, response);
- break;
- case "/ranking":
- ranking(request, response);
- break;
- default:
- response.write("{'error'}");
- }
- }
- function DoGet(request, response, path) {
- }
- function register(request, response) {
- var body = "";
- request.on("data", function(data) {
- body += data;
- });
- request.on("end", function() {
- var data = JSON.parse(body);
- var username = data.nick;
- var password = data.pass;
- if(username == undefined || password == undefined) {
- response.write("{'error'}");
- response.end();
- }
- var info = login.get(username);
- if(info != undefined) {
- if(info != password) {
- response.write("{error: Password Incorrect!}");
- response.end();
- }
- else {
- response.write("{}");
- response.end();
- }
- }
- else {
- login.put(username, password);
- response.write("{}");
- response.end();
- }
- });
- }
- function ranking(request, response) {
- }
- /*
- var parsed = url.parse(request.url, true);
- console.log("parsed?");
- console.log(parsed);
- var pathname = parsed.pathname;
- console.log("pathname?");
- console.log(pathname);
- var query = parsed.query;
- console.log("query?");
- console.log(query);
- var data = {
- game: "asdaejoqwiyehjsadquwe",
- };
- response.write("Tum, ta td");
- response.end();
- */
- server.listen(8041);
- console.log("wat");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement