質問編集履歴

1

index.js ソースコード追加

2017/11/16 02:54

投稿

HiroFumi
HiroFumi

スコア11

test CHANGED
File without changes
test CHANGED
@@ -133,3 +133,83 @@
133
133
 
134
134
 
135
135
  ```
136
+
137
+
138
+
139
+ ■ index.js
140
+
141
+ ```
142
+
143
+ "use strict";
144
+
145
+
146
+
147
+ const express = require('express');
148
+
149
+ const app = express();
150
+
151
+ const fs = require('fs');
152
+
153
+
154
+
155
+ app.set('port', (process.env.PORT || 5000));
156
+
157
+ app.set('views', __dirname + '/views');
158
+
159
+ app.set('view engine', 'ejs');
160
+
161
+
162
+
163
+ fs.readdir('./txt/', (err, files)=>{
164
+
165
+ let idx = '<a href="/">home</a>';
166
+
167
+
168
+
169
+ files.forEach( (v,i,a)=>{
170
+
171
+ if (v.match(/^(?!.).+$/i)) {
172
+
173
+ idx += ' | <a href="/'+v+'">'+v+'</a>';
174
+
175
+ }
176
+
177
+ });
178
+
179
+
180
+
181
+ app.get('/', (req, res)=>{
182
+
183
+ fs.readFile('./home.html', 'utf8', (err,text)=>{
184
+
185
+ res.render('index', {tit : 'home', con : text, lst : idx});
186
+
187
+ });
188
+
189
+ });
190
+
191
+
192
+
193
+ app.get('/:id', (req, res)=>{
194
+
195
+ const ttl = req.params.id;
196
+
197
+ fs.readFile('./txt/'+ttl, 'utf8', (err,text)=>{
198
+
199
+ res.render('index', {tit : ttl, con : text, lst : idx});
200
+
201
+ });
202
+
203
+ });
204
+
205
+ });
206
+
207
+
208
+
209
+ app.listen(app.get('port'), ()=>{
210
+
211
+ console.log("running at localhost:" + app.get('port'))
212
+
213
+ });
214
+
215
+ ```