Skip to content

Commit a4a59c8

Browse files
save file
1 parent 0f70bf1 commit a4a59c8

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

blog/25-07-24/http-server/ex/login-server.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,20 @@
4949

5050
function login(req,res){
5151

52+
if(req.method=='GET'){
53+
res.writeHead(200,{'content-type':'text/html'});
54+
res.end(html.login);
55+
return;
56+
}
57+
5258
var json = post(req);
5359
var user = users.find(user=>user.name===json.name);
5460
if(!user){
55-
res.end('error');
61+
error();
5662
return;
5763
}
5864
if(user.password!==json.password){
59-
res.end('error');
65+
error();
6066
return;
6167
}
6268

@@ -66,8 +72,19 @@
6672
res.statusCode = 303;
6773
res.end('ok');
6874

75+
76+
function error(){
77+
78+
var i = html.login.indexOf('</h4>')+5;
79+
var txt = html.login.slice(0,i)+err+html.login.slice(i+1);
80+
res.writeHead(200,{'content-type':'text/html'});
81+
res.end(txt);
82+
83+
}//erorr
84+
6985
}//login
7086

87+
7188
function logout(req,res){
7289

7390
var json = post(req);
@@ -86,6 +103,7 @@
86103

87104
}//logout
88105

106+
89107
function admin(req,res){
90108

91109
var cookie = req.headers.cookie;
@@ -132,6 +150,7 @@
132150
<a href='admin.html'>admin</a>
133151
</div>
134152
`;
153+
135154
html.login = `
136155
<style>
137156
form{border:1px solid lightgray;padding:10px;display:inline-flex;flex-direction:column;gap:10px}
@@ -154,6 +173,7 @@
154173
</span>
155174
</form>
156175
`;
176+
157177
html.admin = `
158178
<style>
159179
h3{color:green;font-style:italic}
@@ -165,6 +185,7 @@
165185
<a href='logout'>logout</a>
166186
</div>
167187
`;
188+
168189
html.notfound = `
169190
<style>
170191
a{margin:10px}
@@ -173,4 +194,8 @@
173194
<a href='home.html'>home</a>
174195
<a href='login.html'>login</a>
175196
<a href='admin.html'>admin</a>
176-
`;
197+
`;
198+
199+
200+
201+

0 commit comments

Comments
 (0)