OtherPage.html

<!DOCTYPE html>


<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>

    <meta charset="utf-8" />

    <title>Login Page</title>

</head>

<body>

    <h1>Login Page</h1>

    <form method="post">

        <table>

            <tr>

                <td><label>UserName</label></td>

                <td><input type="text" name='login'/></td>

            </tr>

            <tr>

                <td><label>Password</label></td>

                <td><input type="password" name='password' /></td>

            </tr>

        </table>

        <input type="submit"/>

    </form>

</body>

</html>


-----------------


var http = require("http");

var express = require("express");

var app = express();

var fs = require("fs");

var body_parser = require("body-parser");

var cookie_parser = require("cookie-parser");


app.use(body_parser.urlencoded({ extended: false }));

app.use(body_parser.json()); // body를 json 형식으로 만들어줌

app.use(cookie_parser());

app.get("/", function (request, response) {

    if (request.cookies.auth) {

        response.send("<h1>Login Success</h1>");

    } else {

        response.cookies.set("auth", false);

        response.redirect("/login");

    }

});


app.get("/login", function (request, response) {

    fs.readFile("OtherPage.html", function (error, data) {

        response.send(data.toString());

    });

});


app.post("/login", function (request, response) {

    var id = request.body.login;

    var pw = request.body.password;

    

    console.log(id, pw);

    console.log(request.body);

    

    if (id == "rint" && pw == "1234") {

        response.cookies.set("auth", true);

        response.redirect("/");

    } else {

        response.redirect("/login");

    }

});

http.createServer(app).listen(52273, function () {

    console.log("Server running 52273");

});



Posted by 에브리피플
,