質問編集履歴
3
文章の変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,8 +14,6 @@
|
|
14
14
|
|
15
15
|
const router = express.Router();
|
16
16
|
|
17
|
-
const Post = require('./db/db');
|
18
|
-
|
19
17
|
|
20
18
|
|
21
19
|
// add article (./views/new.ejs)
|
@@ -23,10 +21,6 @@
|
|
23
21
|
router.get('/new', function (req, res) {
|
24
22
|
|
25
23
|
res.render('new', {
|
26
|
-
|
27
|
-
title: 'Add article',
|
28
|
-
|
29
|
-
reqCsrf: req.csrfToken(),
|
30
24
|
|
31
25
|
errors: req.flash('errors').shift()
|
32
26
|
|
@@ -36,263 +30,9 @@
|
|
36
30
|
|
37
31
|
|
38
32
|
|
39
|
-
router.post('/create', function (req) {
|
40
|
-
|
41
|
-
const post = new Post();
|
42
|
-
|
43
|
-
post.title = req.body.title;
|
44
|
-
|
45
|
-
post.content = req.body.content;
|
46
|
-
|
47
|
-
post.save(function (err) {
|
48
|
-
|
49
|
-
if(err){ // if there's error
|
50
|
-
|
51
|
-
req.flash('errors', err.errors);
|
52
|
-
|
53
|
-
req.redirect('/new');
|
54
|
-
|
55
|
-
} else { // if not error
|
56
|
-
|
57
|
-
req.redirect('/');
|
58
|
-
|
59
|
-
}
|
60
|
-
|
61
|
-
})
|
62
|
-
|
63
|
-
});
|
64
|
-
|
65
|
-
```
|
66
|
-
|
67
|
-
app.js
|
68
|
-
|
69
|
-
```Javascript
|
70
|
-
|
71
|
-
require('./db/db');
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
const createError = require('http-errors');
|
76
|
-
|
77
|
-
const express = require('express');
|
78
|
-
|
79
|
-
const path = require('path');
|
80
|
-
|
81
|
-
const cookieParser = require('cookie-parser');
|
82
|
-
|
83
|
-
const logger = require('morgan');
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
const session = require('express-session');
|
88
|
-
|
89
|
-
const csurf = require('csurf');
|
90
|
-
|
91
|
-
const mongoose = require('mongoose');
|
92
|
-
|
93
|
-
const flash = require('connect-flash');
|
94
|
-
|
95
|
-
const methodOverride = require('method-override');
|
96
|
-
|
97
|
-
const bodyParser = require('body-parser');
|
98
|
-
|
99
|
-
const MongoStore = require('connect-mongo')(session);
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
const indexRouter = require('./routes/index');
|
106
|
-
|
107
|
-
const usersRouter = require('./routes/users');
|
108
|
-
|
109
|
-
const postRouter = require('./routes/post');
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
const app = express();
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
const mongoURL = 'mongodb://local';
|
120
|
-
|
121
|
-
mongoose.connect(mongoURL, function (err) {
|
122
|
-
|
123
|
-
if(err){
|
124
|
-
|
125
|
-
console.error('mongoDB: error');
|
126
|
-
|
127
|
-
}else{
|
128
|
-
|
129
|
-
console.log('Success connect);
|
130
|
-
|
131
|
-
}
|
132
|
-
|
133
|
-
});
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
app.use(methodOverride(function(req, res){
|
138
|
-
|
139
|
-
if( req.body && typeof req.body === 'object' && '_method' in req.body ){
|
140
|
-
|
141
|
-
const method = req.body._method;
|
142
|
-
|
143
|
-
delete req.body._method;
|
144
|
-
|
145
|
-
return method;
|
146
|
-
|
147
|
-
}
|
148
|
-
|
149
|
-
}));
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
app.use(session({
|
154
|
-
|
155
|
-
secret: 'a',
|
156
|
-
|
157
|
-
resave: false,
|
158
|
-
|
159
|
-
saveUninitialized: false,
|
160
|
-
|
161
|
-
store: new MongoStore({
|
162
|
-
|
163
|
-
mongooseConnection: mongoose.connection
|
164
|
-
|
165
|
-
}),
|
166
|
-
|
167
|
-
cookie: {
|
168
|
-
|
169
|
-
httpOnly: false,
|
170
|
-
|
171
|
-
secure: true,
|
172
|
-
|
173
|
-
maxage: 1000 * 60 * 30 // 30 minutes
|
174
|
-
|
175
|
-
}
|
176
|
-
|
177
|
-
}));
|
178
|
-
|
179
|
-
app.use(csurf());
|
180
|
-
|
181
|
-
app.use(flash());
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
// view engine setup
|
190
|
-
|
191
|
-
app.set('views', path.join(__dirname, 'views'));
|
192
|
-
|
193
|
-
app.set('view engine', 'ejs');
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
app.use(logger('dev'));
|
198
|
-
|
199
|
-
app.use(express.json());
|
200
|
-
|
201
|
-
app.use(bodyParser.json());
|
202
|
-
|
203
|
-
app.use(express.urlencoded({ extended: false }));
|
204
|
-
|
205
|
-
app.use(bodyParser.urlencoded({ extended: false }));
|
206
|
-
|
207
|
-
app.use(cookieParser());
|
208
|
-
|
209
|
-
app.use(express.static(path.join(__dirname, 'public')));
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
app.use('/', indexRouter);
|
218
|
-
|
219
|
-
app.use('/users', usersRouter);
|
220
|
-
|
221
|
-
app.use('/post', postRouter);
|
222
|
-
|
223
|
-
app.listen(process.env.PORT || 8000);
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
// catch 404 and forward to error handler
|
230
|
-
|
231
|
-
app.use(function(req, res, next) {
|
232
|
-
|
233
|
-
next(createError(404));
|
234
|
-
|
235
|
-
});
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
// error handler
|
240
|
-
|
241
|
-
app.use(function(err, req, res, next) {
|
242
|
-
|
243
|
-
// set locals, only providing error in development
|
244
|
-
|
245
|
-
res.locals.message = err.message;
|
246
|
-
|
247
|
-
res.locals.error = req.app.get('env') === 'development' ? err : {};
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
// render the error page
|
252
|
-
|
253
|
-
res.status(err.status || 500);
|
254
|
-
|
255
|
-
res.render('error');
|
256
|
-
|
257
|
-
});
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
module.exports = app;
|
262
|
-
|
263
33
|
```
|
264
34
|
|
265
35
|
|
266
|
-
|
267
|
-
new.ejs
|
268
|
-
|
269
|
-
```HTML
|
270
|
-
|
271
|
-
<form action="/create" method="post">
|
272
|
-
|
273
|
-
<input type="hidden" name="_csrf" value="<%= reqCsrf %>">
|
274
|
-
|
275
|
-
<p>
|
276
|
-
|
277
|
-
<input type="text" name="title" value="" size="60">
|
278
|
-
|
279
|
-
<% if( errors && errors.title ){ %>
|
280
|
-
|
281
|
-
<strong><%= errors.title.message %></strong>
|
282
|
-
|
283
|
-
<% } %>
|
284
|
-
|
285
|
-
</p>
|
286
|
-
|
287
|
-
<p><textarea name="contents" cols="60" rows="12"></textarea></p>
|
288
|
-
|
289
|
-
<p><button type="submit">Create</button></p>
|
290
|
-
|
291
|
-
</form>
|
292
|
-
|
293
|
-
<p><a href="/">back to list</a></p>
|
294
|
-
|
295
|
-
```
|
296
36
|
|
297
37
|
|
298
38
|
|
2
コードを追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -64,6 +64,8 @@
|
|
64
64
|
|
65
65
|
```
|
66
66
|
|
67
|
+
app.js
|
68
|
+
|
67
69
|
```Javascript
|
68
70
|
|
69
71
|
require('./db/db');
|
1
コードを追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
[このサイトを参考にしました](http://webdesign-dackel.com/2015/09/29/vagrant-node-express4-mongodb/)
|
6
6
|
|
7
|
-
|
7
|
+
[GitHUb CSRUF](https://github.com/expressjs/csurf)
|
8
8
|
|
9
9
|
index.js
|
10
10
|
|
@@ -64,6 +64,204 @@
|
|
64
64
|
|
65
65
|
```
|
66
66
|
|
67
|
+
```Javascript
|
68
|
+
|
69
|
+
require('./db/db');
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
const createError = require('http-errors');
|
74
|
+
|
75
|
+
const express = require('express');
|
76
|
+
|
77
|
+
const path = require('path');
|
78
|
+
|
79
|
+
const cookieParser = require('cookie-parser');
|
80
|
+
|
81
|
+
const logger = require('morgan');
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
const session = require('express-session');
|
86
|
+
|
87
|
+
const csurf = require('csurf');
|
88
|
+
|
89
|
+
const mongoose = require('mongoose');
|
90
|
+
|
91
|
+
const flash = require('connect-flash');
|
92
|
+
|
93
|
+
const methodOverride = require('method-override');
|
94
|
+
|
95
|
+
const bodyParser = require('body-parser');
|
96
|
+
|
97
|
+
const MongoStore = require('connect-mongo')(session);
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
const indexRouter = require('./routes/index');
|
104
|
+
|
105
|
+
const usersRouter = require('./routes/users');
|
106
|
+
|
107
|
+
const postRouter = require('./routes/post');
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
const app = express();
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
const mongoURL = 'mongodb://local';
|
118
|
+
|
119
|
+
mongoose.connect(mongoURL, function (err) {
|
120
|
+
|
121
|
+
if(err){
|
122
|
+
|
123
|
+
console.error('mongoDB: error');
|
124
|
+
|
125
|
+
}else{
|
126
|
+
|
127
|
+
console.log('Success connect);
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
});
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
app.use(methodOverride(function(req, res){
|
136
|
+
|
137
|
+
if( req.body && typeof req.body === 'object' && '_method' in req.body ){
|
138
|
+
|
139
|
+
const method = req.body._method;
|
140
|
+
|
141
|
+
delete req.body._method;
|
142
|
+
|
143
|
+
return method;
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
}));
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
app.use(session({
|
152
|
+
|
153
|
+
secret: 'a',
|
154
|
+
|
155
|
+
resave: false,
|
156
|
+
|
157
|
+
saveUninitialized: false,
|
158
|
+
|
159
|
+
store: new MongoStore({
|
160
|
+
|
161
|
+
mongooseConnection: mongoose.connection
|
162
|
+
|
163
|
+
}),
|
164
|
+
|
165
|
+
cookie: {
|
166
|
+
|
167
|
+
httpOnly: false,
|
168
|
+
|
169
|
+
secure: true,
|
170
|
+
|
171
|
+
maxage: 1000 * 60 * 30 // 30 minutes
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
}));
|
176
|
+
|
177
|
+
app.use(csurf());
|
178
|
+
|
179
|
+
app.use(flash());
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
// view engine setup
|
188
|
+
|
189
|
+
app.set('views', path.join(__dirname, 'views'));
|
190
|
+
|
191
|
+
app.set('view engine', 'ejs');
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
app.use(logger('dev'));
|
196
|
+
|
197
|
+
app.use(express.json());
|
198
|
+
|
199
|
+
app.use(bodyParser.json());
|
200
|
+
|
201
|
+
app.use(express.urlencoded({ extended: false }));
|
202
|
+
|
203
|
+
app.use(bodyParser.urlencoded({ extended: false }));
|
204
|
+
|
205
|
+
app.use(cookieParser());
|
206
|
+
|
207
|
+
app.use(express.static(path.join(__dirname, 'public')));
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
app.use('/', indexRouter);
|
216
|
+
|
217
|
+
app.use('/users', usersRouter);
|
218
|
+
|
219
|
+
app.use('/post', postRouter);
|
220
|
+
|
221
|
+
app.listen(process.env.PORT || 8000);
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
// catch 404 and forward to error handler
|
228
|
+
|
229
|
+
app.use(function(req, res, next) {
|
230
|
+
|
231
|
+
next(createError(404));
|
232
|
+
|
233
|
+
});
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
// error handler
|
238
|
+
|
239
|
+
app.use(function(err, req, res, next) {
|
240
|
+
|
241
|
+
// set locals, only providing error in development
|
242
|
+
|
243
|
+
res.locals.message = err.message;
|
244
|
+
|
245
|
+
res.locals.error = req.app.get('env') === 'development' ? err : {};
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
// render the error page
|
250
|
+
|
251
|
+
res.status(err.status || 500);
|
252
|
+
|
253
|
+
res.render('error');
|
254
|
+
|
255
|
+
});
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
module.exports = app;
|
260
|
+
|
261
|
+
```
|
262
|
+
|
263
|
+
|
264
|
+
|
67
265
|
new.ejs
|
68
266
|
|
69
267
|
```HTML
|