Showing
2 changed files
with
20 additions
and
5 deletions
... | @@ -2,6 +2,10 @@ | ... | @@ -2,6 +2,10 @@ |
2 | 2 | ||
3 | const express = require('express'); | 3 | const express = require('express'); |
4 | const app = express(); | 4 | const app = express(); |
5 | +const ejs = require('ejs'); | ||
6 | + | ||
7 | +app.set('view engine', 'ejs'); | ||
8 | +app.set('views', './static'); | ||
5 | 9 | ||
6 | const bodyParser = require('body-parser'); | 10 | const bodyParser = require('body-parser'); |
7 | app.use(bodyParser.urlencoded({extended : true})); | 11 | app.use(bodyParser.urlencoded({extended : true})); |
... | @@ -11,9 +15,17 @@ app.listen(8080, function () { | ... | @@ -11,9 +15,17 @@ app.listen(8080, function () { |
11 | }) | 15 | }) |
12 | 16 | ||
13 | app.get('/', function(req, res){ | 17 | app.get('/', function(req, res){ |
14 | - res.sendFile(__dirname + '/static/index.html') | 18 | + res.render('index.ejs', {items:items}); |
15 | }) | 19 | }) |
16 | 20 | ||
21 | +let items=[]; | ||
22 | + | ||
17 | app.post('/post', function(req, res){ | 23 | app.post('/post', function(req, res){ |
18 | - console.log(req); | 24 | + let item = req.body.text; |
25 | + console.log(item); | ||
26 | + if(item != ""){items.push(item);} | ||
27 | + console.log("items", items); | ||
28 | + | ||
29 | + res.render('index.ejs', {items: items}); | ||
30 | + | ||
19 | }); | 31 | }); |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -4,10 +4,13 @@ | ... | @@ -4,10 +4,13 @@ |
4 | <meta charset="UTF-8"> | 4 | <meta charset="UTF-8"> |
5 | </head> | 5 | </head> |
6 | <body> | 6 | <body> |
7 | - <form action="/post" method="post" name="ti"> | 7 | + <form action="/post" method="post"> |
8 | - <input type="text"> | 8 | + <input type="text" name="text"> |
9 | <button type="submit">submit</button> | 9 | <button type="submit">submit</button> |
10 | </form> | 10 | </form> |
11 | - <div></div> | 11 | + <% items.forEach(function(item){ %> |
12 | + <div> <p> <%= item %> </p></div> | ||
13 | + <% }); %> | ||
14 | + | ||
12 | </body> | 15 | </body> |
13 | </html> | 16 | </html> |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment