0

I'm trying to create a server with an ability to "logon" (the Username and Password must match). I'm trying to do this in "low-level" so to speak, no extensions or anything. Right now when the pages are pulled up they are all accessible and the form displays but when the button is click nothing happens. Nothing is written to the console and check() is never called.

I've tried a lot of things - changing the function, the type of button, the way I'm getting the data from the form and none of it seems to be working. I would really appreciate a push in the right direction (and, total side note, if someone could also talk a moment about how to set a cookie when the user is logged on I would appreciate it as well, but this is very much extra to the question at hand).

var http = require("http");
var url = require("url");

var server = http.createServer(function (req, response){
    var page = url.parse(req.url).pathname;

    response.writeHead(200, {'Content-Type':'text/html'});
    if (page == '/'){
        //home page
    }

    else if (page == '/page1'){
        //page one
    }

else if (page == '/auth'){
        response.write('<html>'+
        '<h1><center>Sign up for dumblr home of the dumblr</center></h1>'+
        '<form method="POST" name="auth" >'+
        'Username: <input type="text" name="user" required /> <br />'+
        'Password: <input type="password" name="pwd" required/> <br />  '+
        '<input type="button" value="Submit" onclick="check(this.form)"/> '+
        '</form>');

        function check(form){
            console.log("1");

            if(form.user.value == form.pwd.value){
                //response.setHeader('Set-Cookie', ['type=ninja', 'language=javascript']);
                console.log("Cookie is set");
                return true;
            }
            else{
                response.write("<h1>Sorry, those are not correct. Try again.</h1>")
                console.log("Didn't work")
                return false;
            }
    }


}
    response.end();

}).listen(3000);

console.log('Server running at http://127.0.0.1:3000');

1 Answer 1

1

You are mixing javascript functions on server and client side.

There's code on the server side that creates a very simple page when you open

http://localhost:3000/auth

But this page on your browser doesn't have a function check(form). This function does not exist on your page, but only on the server side. So when you click on the button, the browser fails to call check and that's it. You should start with a much simpler approach. Just try coding a simple page with javascript only on the client side and try to solve your problem there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.