質問編集履歴
3
解決
test
CHANGED
File without changes
|
test
CHANGED
@@ -5,184 +5,6 @@
|
|
5
5
|
.envから参照する前はエラーは出ませんでした。
|
6
6
|
|
7
7
|
[dotenv](https://www.npmjs.com/package/dotenv)
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
app.js
|
12
|
-
|
13
|
-
``` javascript
|
14
|
-
|
15
|
-
require('./db/db');
|
16
|
-
|
17
|
-
require('dotenv').config();
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
const createError = require('http-errors');
|
22
|
-
|
23
|
-
const express = require('express');
|
24
|
-
|
25
|
-
const path = require('path');
|
26
|
-
|
27
|
-
const cookieParser = require('cookie-parser');
|
28
|
-
|
29
|
-
const logger = require('morgan');
|
30
|
-
|
31
|
-
const mongoose = require('mongoose');
|
32
|
-
|
33
|
-
const flash = require('connect-flash');
|
34
|
-
|
35
|
-
const methodOverride = require('method-override');
|
36
|
-
|
37
|
-
const session = require('express-session');
|
38
|
-
|
39
|
-
const csrf = require('csurf');
|
40
|
-
|
41
|
-
const bodyParser = require('body-parser');
|
42
|
-
|
43
|
-
const MongoStore = require('connect-mongo')(session);
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
const indexRouter = require('./routes/index');
|
50
|
-
|
51
|
-
const usersRouter = require('./routes/users');
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
const app = express();
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
// view engine setup
|
60
|
-
|
61
|
-
app.set('views', path.join(__dirname, 'views'));
|
62
|
-
|
63
|
-
app.set('view engine', 'ejs');
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
app.use(logger('dev'));
|
68
|
-
|
69
|
-
app.use(express.json());
|
70
|
-
|
71
|
-
app.use(express.urlencoded({ extended: false }));
|
72
|
-
|
73
|
-
app.use(cookieParser());
|
74
|
-
|
75
|
-
app.use(express.static(path.join(__dirname, 'public')));
|
76
|
-
|
77
|
-
app.use(bodyParser.json());
|
78
|
-
|
79
|
-
app.use(express.urlencoded({ extended: false }));
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
mongoose.connect(process.env.DB_URL, function (err){
|
86
|
-
|
87
|
-
if(err){
|
88
|
-
|
89
|
-
console.error('error');
|
90
|
-
|
91
|
-
}
|
92
|
-
|
93
|
-
});
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
app.use('/users', usersRouter).
|
100
|
-
|
101
|
-
app.use(methodOverride(function(req){
|
102
|
-
|
103
|
-
if( req.body && typeof req.body === 'object' && '_method' in req.body ){
|
104
|
-
|
105
|
-
const method = req.body._method;
|
106
|
-
|
107
|
-
delete req.body._method;
|
108
|
-
|
109
|
-
return method;
|
110
|
-
|
111
|
-
}
|
112
|
-
|
113
|
-
}));
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
app.use(session({
|
118
|
-
|
119
|
-
secret: process.env.SESSION_SECRET,
|
120
|
-
|
121
|
-
resave: false,
|
122
|
-
|
123
|
-
saveUninitialized: false,
|
124
|
-
|
125
|
-
}));
|
126
|
-
|
127
|
-
app.use(csrf({cookie: true}));
|
128
|
-
|
129
|
-
app.use(flash());
|
130
|
-
|
131
|
-
////////////////////////////////////
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
app.use('/', indexRouter);
|
138
|
-
|
139
|
-
app.use('/users', usersRouter);
|
140
|
-
|
141
|
-
app.use('/', express.static(path.join(__dirname, 'chat')));
|
142
|
-
|
143
|
-
app.listen(process.env.PORT || 1000);
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
// catch 404 and forward to error handler
|
150
|
-
|
151
|
-
app.use(function(req, res, next) {
|
152
|
-
|
153
|
-
next(createError(404));
|
154
|
-
|
155
|
-
});
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
// error handler
|
160
|
-
|
161
|
-
app.use(function(err, req, res, next) {
|
162
|
-
|
163
|
-
// set locals, only providing error in development
|
164
|
-
|
165
|
-
res.locals.message = err.message;
|
166
|
-
|
167
|
-
res.locals.error = req.app.get('env') === 'development' ? err : {};
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
// render the error page
|
172
|
-
|
173
|
-
res.status(err.status || 500);
|
174
|
-
|
175
|
-
res.render('error');
|
176
|
-
|
177
|
-
});
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
module.exports = app;
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
```
|
186
8
|
|
187
9
|
|
188
10
|
|
2
app.js変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,18 +12,176 @@
|
|
12
12
|
|
13
13
|
``` javascript
|
14
14
|
|
15
|
+
require('./db/db');
|
16
|
+
|
17
|
+
require('dotenv').config();
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
const createError = require('http-errors');
|
22
|
+
|
23
|
+
const express = require('express');
|
24
|
+
|
25
|
+
const path = require('path');
|
26
|
+
|
27
|
+
const cookieParser = require('cookie-parser');
|
28
|
+
|
29
|
+
const logger = require('morgan');
|
30
|
+
|
31
|
+
const mongoose = require('mongoose');
|
32
|
+
|
33
|
+
const flash = require('connect-flash');
|
34
|
+
|
35
|
+
const methodOverride = require('method-override');
|
36
|
+
|
37
|
+
const session = require('express-session');
|
38
|
+
|
39
|
+
const csrf = require('csurf');
|
40
|
+
|
41
|
+
const bodyParser = require('body-parser');
|
42
|
+
|
43
|
+
const MongoStore = require('connect-mongo')(session);
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
const indexRouter = require('./routes/index');
|
50
|
+
|
51
|
+
const usersRouter = require('./routes/users');
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
const app = express();
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
// view engine setup
|
60
|
+
|
61
|
+
app.set('views', path.join(__dirname, 'views'));
|
62
|
+
|
63
|
+
app.set('view engine', 'ejs');
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
app.use(logger('dev'));
|
68
|
+
|
69
|
+
app.use(express.json());
|
70
|
+
|
71
|
+
app.use(express.urlencoded({ extended: false }));
|
72
|
+
|
73
|
+
app.use(cookieParser());
|
74
|
+
|
75
|
+
app.use(express.static(path.join(__dirname, 'public')));
|
76
|
+
|
77
|
+
app.use(bodyParser.json());
|
78
|
+
|
79
|
+
app.use(express.urlencoded({ extended: false }));
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
mongoose.connect(process.env.DB_URL, function (err){
|
86
|
+
|
87
|
+
if(err){
|
88
|
+
|
89
|
+
console.error('error');
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
});
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
app.use('/users', usersRouter).
|
100
|
+
|
101
|
+
app.use(methodOverride(function(req){
|
102
|
+
|
103
|
+
if( req.body && typeof req.body === 'object' && '_method' in req.body ){
|
104
|
+
|
105
|
+
const method = req.body._method;
|
106
|
+
|
107
|
+
delete req.body._method;
|
108
|
+
|
109
|
+
return method;
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
}));
|
114
|
+
|
115
|
+
|
116
|
+
|
15
117
|
app.use(session({
|
16
118
|
|
17
|
-
secret: process.env.SESSION_
|
119
|
+
secret: process.env.SESSION_SECRET,
|
18
120
|
|
19
121
|
resave: false,
|
20
122
|
|
21
123
|
saveUninitialized: false,
|
22
124
|
|
23
|
-
}),
|
24
|
-
|
25
125
|
}));
|
26
126
|
|
127
|
+
app.use(csrf({cookie: true}));
|
128
|
+
|
129
|
+
app.use(flash());
|
130
|
+
|
131
|
+
////////////////////////////////////
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
app.use('/', indexRouter);
|
138
|
+
|
139
|
+
app.use('/users', usersRouter);
|
140
|
+
|
141
|
+
app.use('/', express.static(path.join(__dirname, 'chat')));
|
142
|
+
|
143
|
+
app.listen(process.env.PORT || 1000);
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
// catch 404 and forward to error handler
|
150
|
+
|
151
|
+
app.use(function(req, res, next) {
|
152
|
+
|
153
|
+
next(createError(404));
|
154
|
+
|
155
|
+
});
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
// error handler
|
160
|
+
|
161
|
+
app.use(function(err, req, res, next) {
|
162
|
+
|
163
|
+
// set locals, only providing error in development
|
164
|
+
|
165
|
+
res.locals.message = err.message;
|
166
|
+
|
167
|
+
res.locals.error = req.app.get('env') === 'development' ? err : {};
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
// render the error page
|
172
|
+
|
173
|
+
res.status(err.status || 500);
|
174
|
+
|
175
|
+
res.render('error');
|
176
|
+
|
177
|
+
});
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
module.exports = app;
|
182
|
+
|
183
|
+
|
184
|
+
|
27
185
|
```
|
28
186
|
|
29
187
|
|
1
ログの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -38,6 +38,16 @@
|
|
38
38
|
|
39
39
|
|
40
40
|
|
41
|
+
```Log
|
42
|
+
|
43
|
+
2018-04-25T06:56:08.476619+00:00 app[web.1]: GET / 500 1.136 ms - 67
|
44
|
+
|
45
|
+
2018-04-25T06:56:08.477322+00:00 heroku[router]:
|
46
|
+
|
47
|
+
```
|
48
|
+
|
49
|
+
|
50
|
+
|
41
51
|
プログラム自体は動くのですがsecret optionの部分でエラーが出ます。
|
42
52
|
|
43
53
|
どのように書き直せばいいですか?
|