File tree Expand file tree Collapse file tree 1 file changed +28
-3
lines changed
blog/25-07-24/http-server/ex Expand file tree Collapse file tree 1 file changed +28
-3
lines changed Original file line number Diff line number Diff line change 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
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 ) ;
86103
87104 } //logout
88105
106+
89107 function admin ( req , res ) {
90108
91109 var cookie = req . headers . cookie ;
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}
154173 </span>
155174 </form>
156175 ` ;
176+
157177 html . admin = `
158178 <style>
159179 h3{color:green;font-style:italic}
165185 <a href='logout'>logout</a>
166186 </div>
167187 ` ;
188+
168189 html . notfound = `
169190 <style>
170191 a{margin:10px}
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+
You can’t perform that action at this time.
0 commit comments