質問編集履歴

5

変になっていたので変更

2020/06/09 08:39

投稿

aomo_study
aomo_study

スコア3

test CHANGED
File without changes
test CHANGED
@@ -50,544 +50,544 @@
50
50
 
51
51
 
52
52
 
53
+
54
+
55
+ ```JavaScript
56
+
57
+ const express = require('express');
58
+
59
+ const router = express.Router();
60
+
61
+ const uuid = require('uuid');
62
+
63
+ const passwordHash = require('password-hash');
64
+
65
+ const pg = require('pg');
66
+
67
+ const isDebug = true
68
+
69
+
70
+
71
+
72
+
73
+ const pool = new pg.Pool({
74
+
75
+ database: 'dashboard',
76
+
77
+ user: 'postgres',
78
+
79
+ password: 'Kasumin1',
80
+
81
+ host: 'localhost',
82
+
83
+ port: 5432,
84
+
85
+ });
86
+
87
+
88
+
89
+ (略)
90
+
91
+ //------------ ここから先はログイン済み---------------
92
+
93
+ router.all('/authed/*', async function (req, res, next) {
94
+
95
+ console.log("/authed通過")
96
+
97
+ console.log("req.cookies.token=" + req.cookies.token)
98
+
99
+ let client = null
100
+
101
+ let user = null
102
+
103
+ let token = null
104
+
105
+ // トークンチェック
106
+
107
+ //DBアクセスする
108
+
109
+ try{
110
+
111
+ token = req.cookies.token
112
+
113
+ if(!req.cookies.token)throw new Error(1)
114
+
115
+ if(!pool)return res.redirect('/login?mag=5')
116
+
117
+ client = await pool.connect()
118
+
119
+ if(!client)throw new Error(5)
120
+
121
+ user = await client.query('SELECT name FROM user_token WHERE token=$1;',[token])
122
+
123
+ if(client){
124
+
125
+ client.end()
126
+
127
+ }
128
+
129
+ if(!user.rows[0].name)throw new Error(1)
130
+
131
+ req._local = {
132
+
133
+ userInfo: {
134
+
135
+ name: user.rows[0].name
136
+
137
+ }
138
+
139
+ }
140
+
141
+ }catch(err){
142
+
143
+ if(isDebug) console.log(err)
144
+
145
+ msg = err.message
146
+
147
+ if(client){
148
+
149
+ client.end()
150
+
151
+ }
152
+
153
+ return res.redirect('/login?msg='+msg)
154
+
155
+ }
156
+
157
+ next()
158
+
159
+ })
160
+
161
+
162
+
163
+ //----------------------メインページ------------------------
164
+
165
+ router.get('/authed/dashboard',async function (req, res, next) {
166
+
167
+ let posts = null
168
+
169
+ let postnumber = 0
170
+
171
+ let button = 0
172
+
173
+ let msg = ""
174
+
175
+ let page_count = 0
176
+
177
+ let pageCount=null
178
+
179
+ let client = null
180
+
181
+ try{
182
+
183
+ if (!req._local.userInfo.name)return res.redirect('/login?msg=1')
184
+
185
+
186
+
187
+ if(!pool)throw new Error("DBアクセスに失敗しました。")
188
+
189
+ client = await pool.connect()
190
+
191
+ if(!client)throw new Error("DBアクセスに失敗しました。")
192
+
193
+
194
+
195
+ //次へ・戻るボタンが押されたらページを変える
196
+
197
+ if (req.query.page_count)page_count = +req.query.page_count
198
+
199
+
200
+
201
+ //投稿ボタンが押された時のメッセージ
202
+
203
+ if(req.query.msg == 'ok'){
204
+
205
+ msg = "投稿が完了しました!"
206
+
207
+ }else if(req.query.msg == 'ng'){
208
+
209
+ msg = "投稿に失敗しました><"
210
+
211
+ }
212
+
213
+
214
+
215
+
216
+
217
+
218
+
219
+ pageCount = await client.query('SELECT * FROM board_posted ORDER BY number DESC OFFSET 10*$1;',[page_count])
220
+
221
+ if(!pageCount.rowCount)throw new Error("まだ投稿がありません。")
222
+
223
+ if (pageCount.rowCount <= 10) {
224
+
225
+ button = 0
226
+
227
+ } else {
228
+
229
+ button = 1
230
+
231
+ }
232
+
233
+
234
+
235
+ //投稿一覧表示
236
+
237
+ posts = await client.query('SELECT * FROM board_posted ORDER BY number DESC LIMIT 10 OFFSET 10*$1;',[page_count])
238
+
239
+ if(!posts.rows)throw new Error("何もありません。")
240
+
241
+ postnumber = await client.query('SELECT number FROM board_posted ORDER BY number DESC;')
242
+
243
+ console.log("postnumber="+postnumber.rows[0].number)
244
+
245
+
246
+
247
+ }catch(err){
248
+
249
+ console.log("catchしました")
250
+
251
+ msg = err.message
252
+
253
+ if(client){
254
+
255
+ client.end()
256
+
257
+ }
258
+
259
+ return res.redirect('/auther/dashboard?page_count='+page_count)
260
+
261
+ }
262
+
263
+ if(client){
264
+
265
+ client.end()
266
+
267
+ }
268
+
269
+ res.render('dashboard', {
270
+
271
+ username: req._local.userInfo.name,
272
+
273
+ posts: posts.rows,
274
+
275
+ button: button,
276
+
277
+ page_count: page_count,
278
+
279
+ msg: msg,
280
+
281
+ postnumber: postnumber.rows[0].number
282
+
283
+ });
284
+
285
+
286
+
287
+ });
288
+
289
+
290
+
291
+ //-------------------------投稿-------------------------
292
+
293
+ router.post('/newpost',async function (req, res, next) {
294
+
295
+ console.log("req.cookies.token=" + req.cookies.token)
296
+
297
+ let client = null
298
+
299
+ let username=""
300
+
301
+ let msg = ""
302
+
303
+ let postnumber=+req.body.postnumber
304
+
305
+ console.log("postnumber=" + postnumber)
306
+
307
+ try{
308
+
309
+ if(!pool)throw new Error("DBアクセスに失敗しました。")
310
+
311
+ client = await pool.connect()
312
+
313
+ if(!client)throw new Error()
314
+
315
+ //例えばもし同時投稿などで44番目の投稿がすでにあったら、postnumberを45番にする
316
+
317
+ number = await client.query('SELECT *FROM board_posted ORDER BY number DESC')
318
+
319
+ if(number.rows[0].number == postnumber)postnumber++
320
+
321
+ //投稿作業
322
+
323
+ username = await client.query('SELECT name FROM user_token WHERE token=$1',[req.cookies.token])
324
+
325
+ if(!username)throw new Error("usernameなかった")
326
+
327
+ await client.query('INSERT INTO board_posted(number,name,text) VALUES($1,$2,$3)',[postnumber,username.rows[0].name,req.body.text])
328
+
329
+ msg = "ok"
330
+
331
+ }catch(err){
332
+
333
+ console.log("catchしました")
334
+
335
+ if(isDebug) console.log(err)
336
+
337
+ msg = "ng"
338
+
339
+ if(client){
340
+
341
+ client.end()
342
+
343
+ }
344
+
345
+ }
346
+
347
+ if(client){
348
+
349
+ client.end()
350
+
351
+ }
352
+
353
+ res.redirect('/authed/dashboard?msg='+msg)
354
+
355
+ });
356
+
357
+
358
+
359
+ //次へボタンや戻るボタンが押されたら
360
+
361
+ router.post('/next', function (req, res, next) {
362
+
363
+ let page_count
364
+
365
+ try{
366
+
367
+ page_count = Number(req.query.page_count)
368
+
369
+ }catch(err){
370
+
371
+ console.log("catchしました")
372
+
373
+ return res.redirect('/authed/dashboard?page_count='+page_count)
374
+
375
+ }
376
+
377
+ res.redirect('/authed/dashboard?page_count=' + page_count)
378
+
379
+ })
380
+
381
+
382
+
383
+
384
+
385
+ module.exports = router;
386
+
387
+
388
+
389
+
390
+
391
+ // /authed/dahsboard?p=0
392
+
393
+ // 1ページあたりの表示件数 50件 (plen)
394
+
395
+
396
+
397
+ // OFFSET= p * plen LIMIST plen ORDER DESC
398
+
399
+ // 次へ /authed/dahsboard?p=(p+1)
400
+
401
+ // 前へ /authed/dahsboard?p=(p-1)
402
+
403
+ // (p+1) ページ目
404
+
405
+
406
+
53
407
  ```
54
408
 
55
- ```JavaScript
56
-
57
- const express = require('express');
58
-
59
- const router = express.Router();
60
-
61
- const uuid = require('uuid');
62
-
63
- const passwordHash = require('password-hash');
64
-
65
- const pg = require('pg');
66
-
67
- const isDebug = true
68
-
69
-
70
-
71
-
72
-
73
- const pool = new pg.Pool({
74
-
75
- database: 'dashboard',
76
-
77
- user: 'postgres',
78
-
79
- password: 'Kasumin1',
80
-
81
- host: 'localhost',
82
-
83
- port: 5432,
84
-
85
- });
86
-
87
-
88
-
89
- (略)
90
-
91
- //------------ ここから先はログイン済み---------------
92
-
93
- router.all('/authed/*', async function (req, res, next) {
94
-
95
- console.log("/authed通過")
96
-
97
- console.log("req.cookies.token=" + req.cookies.token)
98
-
99
- let client = null
100
-
101
- let user = null
102
-
103
- let token = null
104
-
105
- // トークンチェック
106
-
107
- //DBアクセスする
108
-
109
- try{
110
-
111
- token = req.cookies.token
112
-
113
- if(!req.cookies.token)throw new Error(1)
114
-
115
- if(!pool)return res.redirect('/login?mag=5')
116
-
117
- client = await pool.connect()
118
-
119
- if(!client)throw new Error(5)
120
-
121
- user = await client.query('SELECT name FROM user_token WHERE token=$1;',[token])
122
-
123
- if(client){
124
-
125
- client.end()
126
-
127
- }
128
-
129
- if(!user.rows[0].name)throw new Error(1)
130
-
131
- req._local = {
132
-
133
- userInfo: {
134
-
135
- name: user.rows[0].name
136
-
137
- }
138
-
139
- }
140
-
141
- }catch(err){
142
-
143
- if(isDebug) console.log(err)
144
-
145
- msg = err.message
146
-
147
- if(client){
148
-
149
- client.end()
150
-
151
- }
152
-
153
- return res.redirect('/login?msg='+msg)
154
-
155
- }
156
-
157
- next()
158
-
159
- })
160
-
161
-
162
-
163
- //----------------------メインページ------------------------
164
-
165
- router.get('/authed/dashboard',async function (req, res, next) {
166
-
167
- let posts = null
168
-
169
- let postnumber = 0
170
-
171
- let button = 0
172
-
173
- let msg = ""
174
-
175
- let page_count = 0
176
-
177
- let pageCount=null
178
-
179
- let client = null
180
-
181
- try{
182
-
183
- if (!req._local.userInfo.name)return res.redirect('/login?msg=1')
409
+
410
+
411
+ ```html
412
+
413
+ <!DOCTYPE html>
414
+
415
+ <html>
416
+
417
+
418
+
419
+ <head>
420
+
421
+ <title>Dashboard</title>
422
+
423
+ <link rel='stylesheet' href='/stylesheets/style.css' />
424
+
425
+ <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
426
+
427
+ integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
428
+
429
+
430
+
431
+ </head>
432
+
433
+
434
+
435
+ <body>
436
+
437
+ <header>
438
+
439
+ <div class="navbar navbar-dark bg-dark shadow-sm">
440
+
441
+ <div class="container d-flex justify-content-between">
442
+
443
+ <a class="navbar-brand d-flex align-items-center">
444
+
445
+ <strong>Dashboard</strong>
446
+
447
+ </a>
448
+
449
+ </div>
450
+
451
+ </div>
184
452
 
185
453
 
186
454
 
455
+ </header>
456
+
457
+ <br>
458
+
187
- if(!pool)throw new Error("DBアクセスに失敗しました。")
459
+ <div class="card-deck">
460
+
188
-
461
+ <!------------ユーザープロフィール------------->
462
+
463
+ <div class="card" style="max-width: 18rem; max-height: 18rem;">
464
+
465
+ <div class="card-header">your plofile</div>
466
+
467
+ <div class="card-body">
468
+
469
+ <h4 class="card-title"><%= username %></h4>
470
+
189
- client = await pool.connect()
471
+ <p class="card-text">
472
+
190
-
473
+ This is where your introduction is written.
474
+
475
+ </p>
476
+
477
+ </div>
478
+
191
- if(!client)throw new Error("DBアクセスに失敗しました。")
479
+ <a href="/logout">logout</a>
480
+
481
+ </div>
482
+
483
+
484
+
485
+ <!------------ポストされた投稿一覧-------------->
192
486
 
193
487
 
194
488
 
195
- //次へ・戻るボタンが押されたらページを変える
196
-
197
- if (req.query.page_count)page_count = +req.query.page_count
198
-
199
-
200
-
201
- //投稿ボタンが押された時のメッセージ
202
-
203
- if(req.query.msg == 'ok'){
204
-
205
- msg = "投稿が完了しました!"
206
-
207
- }else if(req.query.msg == 'ng'){
208
-
209
- msg = "投稿に失敗しました><"
210
-
211
- }
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
- pageCount = await client.query('SELECT * FROM board_posted ORDER BY number DESC OFFSET 10*$1;',[page_count])
220
-
221
- if(!pageCount.rowCount)throw new Error("まだ投稿がありません。")
222
-
223
- if (pageCount.rowCount <= 10) {
224
-
225
- button = 0
226
-
227
- } else {
228
-
229
- button = 1
230
-
231
- }
232
-
233
-
234
-
235
- //投稿一覧表示
236
-
237
- posts = await client.query('SELECT * FROM board_posted ORDER BY number DESC LIMIT 10 OFFSET 10*$1;',[page_count])
238
-
239
- if(!posts.rows)throw new Error("何もありません。")
240
-
241
- postnumber = await client.query('SELECT number FROM board_posted ORDER BY number DESC;')
242
-
243
- console.log("postnumber="+postnumber.rows[0].number)
244
-
245
-
246
-
247
- }catch(err){
248
-
249
- console.log("catchしました")
250
-
251
- msg = err.message
252
-
253
- if(client){
254
-
255
- client.end()
256
-
257
- }
258
-
259
- return res.redirect('/auther/dashboard?page_count='+page_count)
260
-
261
- }
262
-
263
- if(client){
264
-
265
- client.end()
266
-
267
- }
268
-
269
- res.render('dashboard', {
270
-
271
- username: req._local.userInfo.name,
272
-
273
- posts: posts.rows,
274
-
275
- button: button,
276
-
277
- page_count: page_count,
278
-
279
- msg: msg,
280
-
281
- postnumber: postnumber.rows[0].number
282
-
283
- });
284
-
285
-
286
-
287
- });
288
-
289
-
290
-
291
- //-------------------------投稿-------------------------
292
-
293
- router.post('/newpost',async function (req, res, next) {
294
-
295
- console.log("req.cookies.token=" + req.cookies.token)
296
-
297
- let client = null
298
-
299
- let username=""
300
-
301
- let msg = ""
302
-
303
- let postnumber=+req.body.postnumber
304
-
305
- console.log("postnumber=" + postnumber)
306
-
307
- try{
308
-
309
- if(!pool)throw new Error("DBアクセスに失敗しました。")
310
-
311
- client = await pool.connect()
312
-
313
- if(!client)throw new Error()
314
-
315
- //例えばもし同時投稿などで44番目の投稿がすでにあったら、postnumberを45番にする
316
-
317
- number = await client.query('SELECT *FROM board_posted ORDER BY number DESC')
318
-
319
- if(number.rows[0].number == postnumber)postnumber++
320
-
321
- //投稿作業
322
-
323
- username = await client.query('SELECT name FROM user_token WHERE token=$1',[req.cookies.token])
324
-
325
- if(!username)throw new Error("usernameなかった")
326
-
327
- await client.query('INSERT INTO board_posted(number,name,text) VALUES($1,$2,$3)',[postnumber,username.rows[0].name,req.body.text])
328
-
329
- msg = "ok"
330
-
331
- }catch(err){
332
-
333
- console.log("catchしました")
334
-
335
- if(isDebug) console.log(err)
336
-
337
- msg = "ng"
338
-
339
- if(client){
340
-
341
- client.end()
342
-
343
- }
344
-
345
- }
346
-
347
- if(client){
348
-
349
- client.end()
350
-
351
- }
352
-
353
- res.redirect('/authed/dashboard?msg='+msg)
354
-
355
- });
356
-
357
-
358
-
359
- //次へボタンや戻るボタンが押されたら
360
-
361
- router.post('/next', function (req, res, next) {
362
-
363
- let page_count
364
-
365
- try{
366
-
367
- page_count = Number(req.query.page_count)
368
-
369
- }catch(err){
370
-
371
- console.log("catchしました")
372
-
373
- return res.redirect('/authed/dashboard?page_count='+page_count)
374
-
375
- }
376
-
377
- res.redirect('/authed/dashboard?page_count=' + page_count)
378
-
379
- })
380
-
381
-
382
-
383
-
384
-
385
- module.exports = router;
386
-
387
-
388
-
389
-
390
-
391
- // /authed/dahsboard?p=0
392
-
393
- // 1ページあたりの表示件数 50件 (plen)
394
-
395
-
396
-
397
- // OFFSET= p * plen LIMIST plen ORDER DESC
398
-
399
- // 次へ /authed/dahsboard?p=(p+1)
400
-
401
- // 前へ /authed/dahsboard?p=(p-1)
402
-
403
- // (p+1) ページ目
404
-
405
-
489
+ <div class="d-flex flex-column bd-highlight mb-3">
490
+
491
+ <div class="row justify-content-center" style='color: red;'><%=msg%></div>
492
+
493
+ <div style='color: red;'></div>
494
+
495
+ <div class="border border-dark" style="padding:10px;background-color:#EEEEEE">
496
+
497
+ <% if (posts != null) { %>
498
+
499
+ <% for(var i= 0; i < posts.length; i++){ %>
500
+
501
+ <div class="card" style="width: 60rem;">
502
+
503
+ <%=posts[i].number%>
504
+
505
+ </div>
506
+
507
+ <div class="card-body">
508
+
509
+ <table border="1">
510
+
511
+ <tbody>
512
+
513
+ <%=posts[i].text%>
514
+
515
+ <tr>
516
+
517
+ <td><%=posts[i].name%></td>
518
+
519
+ </tr>
520
+
521
+
522
+
523
+ </tbody>
524
+
525
+ </table>
526
+
527
+ </div>
528
+
529
+ <%}%>
530
+
531
+ <%}%>
532
+
533
+ </div>
534
+
535
+ <div class="d-flex">
536
+
537
+ <% if(page_count >= 1){ %>
538
+
539
+ <form method="post" action="/next?page_count=<%-page_count -1%>">
540
+
541
+ <button>戻る</button>
542
+
543
+ </form>
544
+
545
+ <% } %>
546
+
547
+ <% if(button == 1){%>
548
+
549
+ <form method="post" action="/next?page_count=<%-page_count +1%>">
550
+
551
+ <button>次へ</button>
552
+
553
+ </form>
554
+
555
+ <% } %>
556
+
557
+ </div>
558
+
559
+ <!--------------------投稿--------------------->
560
+
561
+ <div class="p-2 bd-highlight">
562
+
563
+ <form action="/newpost" method="post">
564
+
565
+ <br>
566
+
567
+ <div><input type="hidden" name="postnumber" value=<%-postnumber+1 %>><%=postnumber+1%>番目の投稿</div>
568
+
569
+ <div>投稿文<br><textarea name="text" cols="50" rows="4" wrap="soft" required></textarea></div>
570
+
571
+ <p>名前:<%=username%></p>
572
+
573
+ <button type="submit">送信</button>
574
+
575
+ </form>
576
+
577
+ </div>
578
+
579
+ </div>
580
+
581
+ </div>
582
+
583
+ </body>
584
+
585
+ </html>
406
586
 
407
587
  ```
408
588
 
409
589
 
410
590
 
411
- ```html
412
-
413
- <!DOCTYPE html>
414
-
415
- <html>
416
-
417
-
418
-
419
- <head>
420
-
421
- <title>Dashboard</title>
422
-
423
- <link rel='stylesheet' href='/stylesheets/style.css' />
424
-
425
- <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
426
-
427
- integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
428
-
429
-
430
-
431
- </head>
432
-
433
-
434
-
435
- <body>
436
-
437
- <header>
438
-
439
- <div class="navbar navbar-dark bg-dark shadow-sm">
440
-
441
- <div class="container d-flex justify-content-between">
442
-
443
- <a class="navbar-brand d-flex align-items-center">
444
-
445
- <strong>Dashboard</strong>
446
-
447
- </a>
448
-
449
- </div>
450
-
451
- </div>
452
-
453
-
454
-
455
- </header>
456
-
457
- <br>
458
-
459
- <div class="card-deck">
460
-
461
- <!------------ユーザープロフィール------------->
462
-
463
- <div class="card" style="max-width: 18rem; max-height: 18rem;">
464
-
465
- <div class="card-header">your plofile</div>
466
-
467
- <div class="card-body">
468
-
469
- <h4 class="card-title"><%= username %></h4>
470
-
471
- <p class="card-text">
472
-
473
- This is where your introduction is written.
474
-
475
- </p>
476
-
477
- </div>
478
-
479
- <a href="/logout">logout</a>
480
-
481
- </div>
482
-
483
-
484
-
485
- <!------------ポストされた投稿一覧-------------->
486
-
487
-
488
-
489
- <div class="d-flex flex-column bd-highlight mb-3">
490
-
491
- <div class="row justify-content-center" style='color: red;'><%=msg%></div>
492
-
493
- <div style='color: red;'></div>
494
-
495
- <div class="border border-dark" style="padding:10px;background-color:#EEEEEE">
496
-
497
- <% if (posts != null) { %>
498
-
499
- <% for(var i= 0; i < posts.length; i++){ %>
500
-
501
- <div class="card" style="width: 60rem;">
502
-
503
- <%=posts[i].number%>
504
-
505
- </div>
506
-
507
- <div class="card-body">
508
-
509
- <table border="1">
510
-
511
- <tbody>
512
-
513
- <%=posts[i].text%>
514
-
515
- <tr>
516
-
517
- <td><%=posts[i].name%></td>
518
-
519
- </tr>
520
-
521
-
522
-
523
- </tbody>
524
-
525
- </table>
526
-
527
- </div>
528
-
529
- <%}%>
530
-
531
- <%}%>
532
-
533
- </div>
534
-
535
- <div class="d-flex">
536
-
537
- <% if(page_count >= 1){ %>
538
-
539
- <form method="post" action="/next?page_count=<%-page_count -1%>">
540
-
541
- <button>戻る</button>
542
-
543
- </form>
544
-
545
- <% } %>
546
-
547
- <% if(button == 1){%>
548
-
549
- <form method="post" action="/next?page_count=<%-page_count +1%>">
550
-
551
- <button>次へ</button>
552
-
553
- </form>
554
-
555
- <% } %>
556
-
557
- </div>
558
-
559
- <!--------------------投稿--------------------->
560
-
561
- <div class="p-2 bd-highlight">
562
-
563
- <form action="/newpost" method="post">
564
-
565
- <br>
566
-
567
- <div><input type="hidden" name="postnumber" value=<%-postnumber+1 %>><%=postnumber+1%>番目の投稿</div>
568
-
569
- <div>投稿文<br><textarea name="text" cols="50" rows="4" wrap="soft" required></textarea></div>
570
-
571
- <p>名前:<%=username%></p>
572
-
573
- <button type="submit">送信</button>
574
-
575
- </form>
576
-
577
- </div>
578
-
579
- </div>
580
-
581
- </div>
582
-
583
- </body>
584
-
585
- </html>
586
-
587
- ```
588
-
589
-
590
-
591
591
  ### 試したこと
592
592
 
593
593
 

4

質問内容の変更

2020/06/09 08:39

投稿

aomo_study
aomo_study

スコア3

test CHANGED
File without changes
test CHANGED
@@ -40,7 +40,9 @@
40
40
 
41
41
  ```
42
42
 
43
- と出てきます。
43
+ と出てきて止ります。
44
+
45
+ 原因がわかりません。
44
46
 
45
47
 
46
48
 

3

開発環境の詳細記載

2020/06/09 08:38

投稿

aomo_study
aomo_study

スコア3

test CHANGED
File without changes
test CHANGED
@@ -596,8 +596,14 @@
596
596
 
597
597
  ### 開発環境
598
598
 
599
-
600
-
601
599
  windows10
602
600
 
603
601
  VSCode
602
+
603
+ PostgreSQL
604
+
605
+ Node.js
606
+
607
+ JavaScript
608
+
609
+ Express

2

質問とタイトルの変更

2020/06/09 08:37

投稿

aomo_study
aomo_study

スコア3

test CHANGED
@@ -1 +1 @@
1
- 何度もDBにアクセスするとそのうち動かなくなってしまい、原因わかりせん(Node.js)
1
+ 何度もDBにアクセスするとERR_EMPTY_RESPONSEエラー
test CHANGED
@@ -10,11 +10,11 @@
10
10
 
11
11
  ExpressとNode.jsとPostgreSQLで、掲示板Webページを開発しています。
12
12
 
13
- 以下のようになってしまい、先に進みせん
13
+ 以下のようになってしまいま
14
14
 
15
15
  質問の仕方が分かりずらいかもしれませんが、お力を貸してください。
16
16
 
17
- プログラムがエラーを吐いているわけではないです。
17
+
18
18
 
19
19
 
20
20
 
@@ -24,9 +24,23 @@
24
24
 
25
25
 
26
26
 
27
+ ######何度かアクセス(ページを進めたり投稿したり…)を繰り返すと、
28
+
27
- ######何度かアクセス(ページを進めたり投稿したり…)を繰り返すと、「localhostにアクセスしています」という文字がWebページ左下に出て止まり、先に進みません。
29
+ 「localhostにアクセスしています」という文字がWebページ左下に出て止まり、先に進みません。
30
+
28
-
31
+ そしてしばらく待った後、
32
+
33
+ ```
34
+
35
+ このページは動作していません
36
+
29
- 調べたところ、pool.connect()のところでいつも止まってしまいます。DBにアクセスできていないことはわったのです、何が悪いのかわかりません。
37
+ localhost らデータ送信されませんでした
38
+
39
+ ERR_EMPTY_RESPONSE
40
+
41
+ ```
42
+
43
+ と出てきます。
30
44
 
31
45
 
32
46
 

1

プログラム略所の修正

2020/06/09 08:34

投稿

aomo_study
aomo_study

スコア3

test CHANGED
File without changes
test CHANGED
@@ -70,306 +70,300 @@
70
70
 
71
71
 
72
72
 
73
+ (略)
74
+
75
+ //------------ ここから先はログイン済み---------------
76
+
73
- router.get('/', function (req, res, next) {
77
+ router.all('/authed/*', async function (req, res, next) {
78
+
74
-
79
+ console.log("/authed通過")
80
+
81
+ console.log("req.cookies.token=" + req.cookies.token)
82
+
83
+ let client = null
84
+
85
+ let user = null
86
+
87
+ let token = null
88
+
89
+ // トークンチェック
90
+
91
+ //DBアクセスする
92
+
93
+ try{
94
+
95
+ token = req.cookies.token
96
+
97
+ if(!req.cookies.token)throw new Error(1)
98
+
99
+ if(!pool)return res.redirect('/login?mag=5')
100
+
101
+ client = await pool.connect()
102
+
103
+ if(!client)throw new Error(5)
104
+
105
+ user = await client.query('SELECT name FROM user_token WHERE token=$1;',[token])
106
+
107
+ if(client){
108
+
109
+ client.end()
110
+
111
+ }
112
+
113
+ if(!user.rows[0].name)throw new Error(1)
114
+
115
+ req._local = {
116
+
117
+ userInfo: {
118
+
119
+ name: user.rows[0].name
120
+
121
+ }
122
+
123
+ }
124
+
125
+ }catch(err){
126
+
127
+ if(isDebug) console.log(err)
128
+
129
+ msg = err.message
130
+
131
+ if(client){
132
+
133
+ client.end()
134
+
135
+ }
136
+
137
+ return res.redirect('/login?msg='+msg)
138
+
139
+ }
140
+
141
+ next()
142
+
143
+ })
144
+
145
+
146
+
147
+ //----------------------メインページ------------------------
148
+
149
+ router.get('/authed/dashboard',async function (req, res, next) {
150
+
151
+ let posts = null
152
+
153
+ let postnumber = 0
154
+
155
+ let button = 0
156
+
157
+ let msg = ""
158
+
159
+ let page_count = 0
160
+
161
+ let pageCount=null
162
+
163
+ let client = null
164
+
165
+ try{
166
+
167
+ if (!req._local.userInfo.name)return res.redirect('/login?msg=1')
168
+
169
+
170
+
171
+ if(!pool)throw new Error("DBアクセスに失敗しました。")
172
+
173
+ client = await pool.connect()
174
+
175
+ if(!client)throw new Error("DBアクセスに失敗しました。")
176
+
177
+
178
+
179
+ //次へ・戻るボタンが押されたらページを変える
180
+
181
+ if (req.query.page_count)page_count = +req.query.page_count
182
+
183
+
184
+
185
+ //投稿ボタンが押された時のメッセージ
186
+
187
+ if(req.query.msg == 'ok'){
188
+
189
+ msg = "投稿が完了しました!"
190
+
191
+ }else if(req.query.msg == 'ng'){
192
+
193
+ msg = "投稿に失敗しました><"
194
+
195
+ }
196
+
197
+
198
+
199
+
200
+
201
+
202
+
203
+ pageCount = await client.query('SELECT * FROM board_posted ORDER BY number DESC OFFSET 10*$1;',[page_count])
204
+
205
+ if(!pageCount.rowCount)throw new Error("まだ投稿がありません。")
206
+
207
+ if (pageCount.rowCount <= 10) {
208
+
209
+ button = 0
210
+
211
+ } else {
212
+
213
+ button = 1
214
+
215
+ }
216
+
217
+
218
+
219
+ //投稿一覧表示
220
+
221
+ posts = await client.query('SELECT * FROM board_posted ORDER BY number DESC LIMIT 10 OFFSET 10*$1;',[page_count])
222
+
223
+ if(!posts.rows)throw new Error("何もありません。")
224
+
225
+ postnumber = await client.query('SELECT number FROM board_posted ORDER BY number DESC;')
226
+
227
+ console.log("postnumber="+postnumber.rows[0].number)
228
+
229
+
230
+
231
+ }catch(err){
232
+
233
+ console.log("catchしました")
234
+
235
+ msg = err.message
236
+
237
+ if(client){
238
+
239
+ client.end()
240
+
241
+ }
242
+
243
+ return res.redirect('/auther/dashboard?page_count='+page_count)
244
+
245
+ }
246
+
247
+ if(client){
248
+
249
+ client.end()
250
+
251
+ }
252
+
75
- res.render('index');
253
+ res.render('dashboard', {
254
+
255
+ username: req._local.userInfo.name,
256
+
257
+ posts: posts.rows,
258
+
259
+ button: button,
260
+
261
+ page_count: page_count,
262
+
263
+ msg: msg,
264
+
265
+ postnumber: postnumber.rows[0].number
266
+
267
+ });
268
+
269
+
76
270
 
77
271
  });
78
272
 
79
- (略)
273
+
80
-
274
+
81
- //------------ ここから先はログイン済み---------------
275
+ //-------------------------投稿-------------------------
82
-
276
+
83
- router.all('/authed/*', async function (req, res, next) {
277
+ router.post('/newpost',async function (req, res, next) {
84
-
85
- console.log("/authed通過")
86
278
 
87
279
  console.log("req.cookies.token=" + req.cookies.token)
88
280
 
89
281
  let client = null
90
282
 
91
- let user = null
283
+ let username=""
92
-
284
+
93
- let token = null
285
+ let msg = ""
94
-
286
+
95
- // トークンチェック
287
+ let postnumber=+req.body.postnumber
96
-
288
+
97
- //DBアクセスする
289
+ console.log("postnumber=" + postnumber)
98
290
 
99
291
  try{
100
292
 
101
- token = req.cookies.token
102
-
103
- if(!req.cookies.token)throw new Error(1)
104
-
105
- if(!pool)return res.redirect('/login?mag=5')
293
+ if(!pool)throw new Error("DBアクセスに失敗しました。")
106
294
 
107
295
  client = await pool.connect()
108
296
 
109
- if(!client)throw new Error(5)
297
+ if(!client)throw new Error()
298
+
110
-
299
+ //例えばもし同時投稿などで44番目の投稿がすでにあったら、postnumberを45番にする
300
+
301
+ number = await client.query('SELECT *FROM board_posted ORDER BY number DESC')
302
+
303
+ if(number.rows[0].number == postnumber)postnumber++
304
+
305
+ //投稿作業
306
+
111
- user = await client.query('SELECT name FROM user_token WHERE token=$1;',[token])
307
+ username = await client.query('SELECT name FROM user_token WHERE token=$1',[req.cookies.token])
308
+
309
+ if(!username)throw new Error("usernameなかった")
310
+
311
+ await client.query('INSERT INTO board_posted(number,name,text) VALUES($1,$2,$3)',[postnumber,username.rows[0].name,req.body.text])
312
+
313
+ msg = "ok"
314
+
315
+ }catch(err){
316
+
317
+ console.log("catchしました")
318
+
319
+ if(isDebug) console.log(err)
320
+
321
+ msg = "ng"
112
322
 
113
323
  if(client){
114
324
 
325
+ client.end()
326
+
327
+ }
328
+
329
+ }
330
+
331
+ if(client){
332
+
115
333
  client.end()
116
334
 
117
- }
335
+ }
336
+
118
-
337
+ res.redirect('/authed/dashboard?msg='+msg)
338
+
339
+ });
340
+
341
+
342
+
343
+ //次へボタンや戻るボタンが押されたら
344
+
119
- if(!user.rows[0].name)throw new Error(1)
345
+ router.post('/next', function (req, res, next) {
120
-
346
+
121
- req._local = {
347
+ let page_count
122
-
348
+
123
- userInfo: {
349
+ try{
124
-
350
+
125
- name: user.rows[0].name
351
+ page_count = Number(req.query.page_count)
126
-
127
- }
128
-
129
- }
130
352
 
131
353
  }catch(err){
132
354
 
133
- if(isDebug) console.log(err)
355
+ console.log("catchしました")
134
-
135
- msg = err.message
356
+
136
-
137
- if(client){
138
-
139
- client.end()
140
-
141
- }
142
-
143
- return res.redirect('/login?msg='+msg)
357
+ return res.redirect('/authed/dashboard?page_count='+page_count)
144
358
 
145
359
  }
146
360
 
147
- next()
361
+ res.redirect('/authed/dashboard?page_count=' + page_count)
148
362
 
149
363
  })
150
364
 
151
365
 
152
366
 
153
- //----------------------メインページ------------------------
154
-
155
- router.get('/authed/dashboard',async function (req, res, next) {
156
-
157
- let posts = null
158
-
159
- let postnumber = 0
160
-
161
- let button = 0
162
-
163
- let msg = ""
164
-
165
- let page_count = 0
166
-
167
- let pageCount=null
168
-
169
- let client = null
170
-
171
- try{
172
-
173
- if (!req._local.userInfo.name)return res.redirect('/login?msg=1')
174
-
175
-
176
-
177
- if(!pool)throw new Error("DBアクセスに失敗しました。")
178
-
179
- client = await pool.connect()
180
-
181
- if(!client)throw new Error("DBアクセスに失敗しました。")
182
-
183
-
184
-
185
- //次へ・戻るボタンが押されたらページを変える
186
-
187
- if (req.query.page_count)page_count = +req.query.page_count
188
-
189
-
190
-
191
- //投稿ボタンが押された時のメッセージ
192
-
193
- if(req.query.msg == 'ok'){
194
-
195
- msg = "投稿が完了しました!"
196
-
197
- }else if(req.query.msg == 'ng'){
198
-
199
- msg = "投稿に失敗しました><"
200
-
201
- }
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
- pageCount = await client.query('SELECT * FROM board_posted ORDER BY number DESC OFFSET 10*$1;',[page_count])
210
-
211
- if(!pageCount.rowCount)throw new Error("まだ投稿がありません。")
212
-
213
- if (pageCount.rowCount <= 10) {
214
-
215
- button = 0
216
-
217
- } else {
218
-
219
- button = 1
220
-
221
- }
222
-
223
-
224
-
225
- //投稿一覧表示
226
-
227
- posts = await client.query('SELECT * FROM board_posted ORDER BY number DESC LIMIT 10 OFFSET 10*$1;',[page_count])
228
-
229
- if(!posts.rows)throw new Error("何もありません。")
230
-
231
- postnumber = await client.query('SELECT number FROM board_posted ORDER BY number DESC;')
232
-
233
- console.log("postnumber="+postnumber.rows[0].number)
234
-
235
-
236
-
237
- }catch(err){
238
-
239
- console.log("catchしました")
240
-
241
- msg = err.message
242
-
243
- if(client){
244
-
245
- client.end()
246
-
247
- }
248
-
249
- return res.redirect('/auther/dashboard?page_count='+page_count)
250
-
251
- }
252
-
253
- if(client){
254
-
255
- client.end()
256
-
257
- }
258
-
259
- res.render('dashboard', {
260
-
261
- username: req._local.userInfo.name,
262
-
263
- posts: posts.rows,
264
-
265
- button: button,
266
-
267
- page_count: page_count,
268
-
269
- msg: msg,
270
-
271
- postnumber: postnumber.rows[0].number
272
-
273
- });
274
-
275
-
276
-
277
- });
278
-
279
-
280
-
281
- //-------------------------投稿-------------------------
282
-
283
- router.post('/newpost',async function (req, res, next) {
284
-
285
- console.log("req.cookies.token=" + req.cookies.token)
286
-
287
- let client = null
288
-
289
- let username=""
290
-
291
- let msg = ""
292
-
293
- let postnumber=+req.body.postnumber
294
-
295
- console.log("postnumber=" + postnumber)
296
-
297
- try{
298
-
299
- if(!pool)throw new Error("DBアクセスに失敗しました。")
300
-
301
- client = await pool.connect()
302
-
303
- if(!client)throw new Error()
304
-
305
- //例えばもし同時投稿などで44番目の投稿がすでにあったら、postnumberを45番にする
306
-
307
- number = await client.query('SELECT *FROM board_posted ORDER BY number DESC')
308
-
309
- if(number.rows[0].number == postnumber)postnumber++
310
-
311
- //投稿作業
312
-
313
- username = await client.query('SELECT name FROM user_token WHERE token=$1',[req.cookies.token])
314
-
315
- if(!username)throw new Error("usernameなかった")
316
-
317
- await client.query('INSERT INTO board_posted(number,name,text) VALUES($1,$2,$3)',[postnumber,username.rows[0].name,req.body.text])
318
-
319
- msg = "ok"
320
-
321
- }catch(err){
322
-
323
- console.log("catchしました")
324
-
325
- if(isDebug) console.log(err)
326
-
327
- msg = "ng"
328
-
329
- if(client){
330
-
331
- client.end()
332
-
333
- }
334
-
335
- }
336
-
337
- if(client){
338
-
339
- client.end()
340
-
341
- }
342
-
343
- res.redirect('/authed/dashboard?msg='+msg)
344
-
345
- });
346
-
347
-
348
-
349
- //次へボタンや戻るボタンが押されたら
350
-
351
- router.post('/next', function (req, res, next) {
352
-
353
- let page_count
354
-
355
- try{
356
-
357
- page_count = Number(req.query.page_count)
358
-
359
- }catch(err){
360
-
361
- console.log("catchしました")
362
-
363
- return res.redirect('/authed/dashboard?page_count='+page_count)
364
-
365
- }
366
-
367
- res.redirect('/authed/dashboard?page_count=' + page_count)
368
-
369
- })
370
-
371
-
372
-
373
367
 
374
368
 
375
369
  module.exports = router;