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,21 @@ 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 ( "{warning}" , "入力されたユーザー名は存在しません。" ) ;
3328 response . send ( html ) ;
3429 } else if ( user . password === request . body . password ) {
3530 const sessionId = await client . Session . findFirst ( {
@@ -41,57 +36,47 @@ app.post("/login", async (request, response) => {
4136 where : { id : sessionId . id } ,
4237 } ) ;
4338 response . cookie ( "session" , sessionId . id ) ;
44- const html = ejs . render ( login , {
45- username : prof . name ,
46- } ) ;
39+ const html = login . replace ( "{username}" , prof . username ) ;
4740 response . send ( html ) ;
4841 } else {
49- const html = ejs . render ( index , {
50- warning : "パスワードが違います。" ,
51- } ) ;
42+ const html = index . replace ( "{warning}" , "パスワードが違います。" ) ;
5243 response . send ( html ) ;
5344 }
5445} ) ;
5546
5647app . get ( "/profile" , async ( request , response ) => {
57- const profile = fs . readFileSync ( "profile.ejs " , "utf-8" ) ;
48+ const profile = fs . readFileSync ( "profile.html " , "utf-8" ) ;
5849 const prof = await client . Profile . findUnique ( {
5950 where : { id : request . cookies . session } ,
6051 } ) ;
61- const html = ejs . render ( profile , {
62- name : prof . name ,
63- age : prof . age ,
64- univ : prof . univ ,
65- } ) ;
52+ const html = profile . replace ( "{name}" , prof . name )
53+ . replace ( "{age}" , prof . age )
54+ . replace ( "{univ}" , prof . univ ) ;
6655 response . send ( html ) ;
6756} ) ;
6857
69- app . get ( "/resister" , ( request , response ) => {
70- const resister = fs . readFileSync ( "resister.ejs" , "utf-8" ) ;
71- const html = ejs . render ( resister , {
72- warning : "" ,
73- } ) ;
58+ app . get ( "/register" , ( request , response ) => {
59+ const register = fs . readFileSync ( "register.html" , "utf-8" ) ;
60+ const html = register . replace ( "{warning}" , "" ) ;
7461 response . send ( html ) ;
7562} ) ;
7663
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" ) ;
64+ app . post ( "/registered " , async ( request , response ) => {
65+ const index = fs . readFileSync ( "index.html " , "utf-8" ) ;
66+ const register = fs . readFileSync ( "register.html " , "utf-8" ) ;
8067 const user = await client . User . findUnique ( {
8168 where : {
8269 username : request . body . username ,
8370 } ,
8471 } ) ;
8572 if (
86- ( request . body . username === "" ) |
87- ( request . body . password === "" ) |
88- ( request . body . name === "" ) |
89- ( request . body . age === "" ) |
73+ ( request . body . username === "" ) ||
74+ ( request . body . password === "" ) ||
75+ ( request . body . name === "" ) ||
76+ ( request . body . age === "" ) ||
9077 ( request . body . univ === "" )
9178 ) {
92- const html = ejs . render ( resister , {
93- warning : "未記入の項目があります。" ,
94- } ) ;
79+ const html = register . replace ( "{warning}" , "未記入の項目があります。" ) ;
9580 response . send ( html ) ;
9681 } else if ( user === undefined ) {
9782 const new_user = await client . User . create ( {
@@ -114,15 +99,10 @@ app.post("/resistered", async (request, response) => {
11499 univ : request . body . univ ,
115100 } ,
116101 } ) ;
117- const html = ejs . render ( index , {
118- warning : "登録が完了しました。" ,
119- } ) ;
102+ const html = index . replace ( "{warning}" , "登録が完了しました。" ) ;
120103 response . send ( html ) ;
121104 } else {
122- const html = ejs . render ( resister , {
123- warning :
124- "入力されたユーザー名はすでに使用されています。別のユーザー名を入力してください。" ,
125- } ) ;
105+ const html = register . replace ( "{warning}" , "入力されたユーザー名はすでに使用されています。別のユーザー名を入力してください。" )
126106 response . send ( html ) ;
127107 }
128108} ) ;
0 commit comments