Thursday 20 April 2017

No response when using commandline to execute NODE.js script

I am trying to do a script where the user would access the local website and paste their encrypted text. However when I am executing the following code, no response it showing up when the user submits the text. I am trying to show the inputted text using the console.log(reqbody) however nothing shows up. Help is very appreciated thanks!

const http = require('http')
var qs = require("querystring");
const port = 9000

function getHome(request, response) {
    response.writeHead(200, { "Content-Type": "text/html" });
    response.write("<html><body>Encrypt your email! <a 
href='http://travistidwell.com/jsencrypt/demo/index.html'>Click.</a> Verify 
Wi-Fi AP <a href='/send'>Click here.</a></body ></html > ")
    response.end();
}

function get404(request, response) {
    response.writeHead(404, "Resource Not Found", { "Content-Type": 
"text/html" });
    response.write("<html><title> Error 404 </title><h1>404: Resource not 
found!</h1></html >")
    response.end();
}

function get405(request, response) {
    response.writeHead(405, "Method not supported", { "Content-Type": 
"text/html" });
    response.write("<html><title> Error 405</title><h1>Error 405: Method not 
supported.</h1></html >")
    response.end();
}

function getSendForm(request, response) {
    response.write("<html><body><form method='post'><form><tr><td>Enter the 
encrypted text:</td><td><input type='text' id='encryptedtext' value='Paste 
encrypted text.'/></td></tr><tr><td><input type='submit' value='Send'/></td>
</tr></form> </body></html>")  
    };

    http.createServer(function (request, response) {
        console.log(request.url);
        switch (request.method) {
            case "GET":
                if (request.url === "/") {
                    getHome(request, response);
                }
                else if (request.url === "/send") {
                    getSendForm(request, response);
                }
                else {
                    get404(request, response);
                }
                break;
            case "POST":
                if (request.url === "/send") {
                    var reqBody = '';
                    request.on('data', function (data) {
                        reqBody += data;
                        if (reqBody.length > 1e7) //10mb
                        {
                            response.writeHead(413, 'Too large encrypted 
data.', { 'Content-Type': 'text/html' });
                            response.write("<html><title> Error 413 </title>
<h1>413: Too much data.</h1></html >")
                            response.end();
                        }
                    });
                    request.on('end', function (data) {
                        console.log(reqBody);
                    });
                }
                else {
                    get404(request, response);
                }
                break;
           default:
                get405(request, response);
                break;
        }
    }).listen(port);
    console.log("server")



via user3108169

No comments:

Post a Comment