11import express from "express" ;
22import cookieParser from "cookie-parser" ;
33import fs from "fs" ;
4- import ejs from "ejs" ;
54
65const app = express ( ) ;
76app . use ( express . urlencoded ( { extended : true } ) ) ;
@@ -11,25 +10,24 @@ import { PrismaClient } from "@prisma/client";
1110const client = new PrismaClient ( ) ;
1211
1312app . get ( "/" , ( request , response ) => {
14- const index = fs . readFileSync ( "index.ejs" , "utf-8" ) ;
15- const html = ejs . render ( index , {
16- warning : "" ,
17- } ) ;
13+ const index = fs . readFileSync ( "index.html" , "utf-8" ) ;
14+ const html = index . replace ( "{warning}" , "" ) ;
1815 response . send ( html ) ;
1916} ) ;
2017
2118app . post ( "/login" , async ( request , response ) => {
22- const login = fs . readFileSync ( "login.ejs " , "utf-8" ) ;
23- const index = fs . readFileSync ( "index.ejs " , "utf-8" ) ;
19+ const login = fs . readFileSync ( "login.html " , "utf-8" ) ;
20+ const index = fs . readFileSync ( "index.html " , "utf-8" ) ;
2421 const user = await client . User . findUnique ( {
2522 where : {
2623 username : request . body . username ,
2724 } ,
2825 } ) ;
2926 if ( user === undefined ) {
30- const html = ejs . render ( index , {
31- warning : "入力されたユーザー名は存在しません。" ,
32- } ) ;
27+ const html = index . replace (
28+ "{warning}" ,
29+ "入力されたユーザー名は存在しません。" ,
30+ ) ;
3331 response . send ( html ) ;
3432 } else if ( user . password === request . body . password ) {
3533 const sessionId = await client . Session . findFirst ( {
@@ -41,57 +39,48 @@ app.post("/login", async (request, response) => {
4139 where : { id : sessionId . id } ,
4240 } ) ;
4341 response . cookie ( "session" , sessionId . id ) ;
44- const html = ejs . render ( login , {
45- username : prof . name ,
46- } ) ;
42+ const html = login . replace ( "{username}" , prof . username ) ;
4743 response . send ( html ) ;
4844 } else {
49- const html = ejs . render ( index , {
50- warning : "パスワードが違います。" ,
51- } ) ;
45+ const html = index . replace ( "{warning}" , "パスワードが違います。" ) ;
5246 response . send ( html ) ;
5347 }
5448} ) ;
5549
5650app . get ( "/profile" , async ( request , response ) => {
57- const profile = fs . readFileSync ( "profile.ejs " , "utf-8" ) ;
51+ const profile = fs . readFileSync ( "profile.html " , "utf-8" ) ;
5852 const prof = await client . Profile . findUnique ( {
5953 where : { id : request . cookies . session } ,
6054 } ) ;
61- const html = ejs . render ( profile , {
62- name : prof . name ,
63- age : prof . age ,
64- univ : prof . univ ,
65- } ) ;
55+ const html = profile
56+ . replace ( "{name}" , prof . name )
57+ . replace ( "{age}" , prof . age )
58+ . replace ( "{univ}" , prof . univ ) ;
6659 response . send ( html ) ;
6760} ) ;
6861
69- app . get ( "/resister" , ( request , response ) => {
70- const resister = fs . readFileSync ( "resister.ejs" , "utf-8" ) ;
71- const html = ejs . render ( resister , {
72- warning : "" ,
73- } ) ;
62+ app . get ( "/register" , ( request , response ) => {
63+ const register = fs . readFileSync ( "register.html" , "utf-8" ) ;
64+ const html = register . replace ( "{warning}" , "" ) ;
7465 response . send ( html ) ;
7566} ) ;
7667
77- app . post ( "/resistered " , async ( request , response ) => {
78- const index = fs . readFileSync ( "index.ejs " , "utf-8" ) ;
79- const resister = fs . readFileSync ( "resister.ejs " , "utf-8" ) ;
68+ app . post ( "/registered " , async ( request , response ) => {
69+ const index = fs . readFileSync ( "index.html " , "utf-8" ) ;
70+ const register = fs . readFileSync ( "register.html " , "utf-8" ) ;
8071 const user = await client . User . findUnique ( {
8172 where : {
8273 username : request . body . username ,
8374 } ,
8475 } ) ;
8576 if (
86- ( request . body . username === "" ) |
87- ( request . body . password === "" ) |
88- ( request . body . name === "" ) |
89- ( request . body . age === "" ) |
90- ( request . body . univ === "" )
77+ request . body . username === "" | |
78+ request . body . password === "" | |
79+ request . body . name === "" | |
80+ request . body . age === "" | |
81+ request . body . univ === ""
9182 ) {
92- const html = ejs . render ( resister , {
93- warning : "未記入の項目があります。" ,
94- } ) ;
83+ const html = register . replace ( "{warning}" , "未記入の項目があります。" ) ;
9584 response . send ( html ) ;
9685 } else if ( user === undefined ) {
9786 const new_user = await client . User . create ( {
@@ -114,15 +103,13 @@ app.post("/resistered", async (request, response) => {
114103 univ : request . body . univ ,
115104 } ,
116105 } ) ;
117- const html = ejs . render ( index , {
118- warning : "登録が完了しました。" ,
119- } ) ;
106+ const html = index . replace ( "{warning}" , "登録が完了しました。" ) ;
120107 response . send ( html ) ;
121108 } else {
122- const html = ejs . render ( resister , {
123- warning :
124- "入力されたユーザー名はすでに使用されています。別のユーザー名を入力してください。" ,
125- } ) ;
109+ const html = register . replace (
110+ "{ warning}" ,
111+ "入力されたユーザー名はすでに使用されています。別のユーザー名を入力してください。" ,
112+ ) ;
126113 response . send ( html ) ;
127114 }
128115} ) ;
0 commit comments