채지성

third

......@@ -2,30 +2,39 @@
const express = require('express');
const app = express();
const ejs = require('ejs');
app.set('view engine', 'ejs');
app.set('views', './static');
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended : true}));
app.use(express.json());
app.listen(8080, function () {
console.log('listening on 8080')
})
app.get('/', function(req, res){
res.render('index.ejs', {items:items});
})
let items=[];
app.get('/', function(req, res){
res.send(items);
});
app.post('/post', function(req, res){
let item = req.body.text;
console.log(item);
if(item != ""){items.push(item);}
console.log("items", items);
let item = req.body;
console.log(item.id);
console.log(item.text);
if(item != ""){ items.push(item); }
console.log("items: ", items);
res.send(items);
});
app.delete('/item/:id', function(req, res){
let id = req.params.id;
console.log(id);
res.render('index.ejs', {items: items});
items.forEach((elem, index)=>{
if(elem.id == id){ items.splice(index, 1); }
});
console.log(items);
res.send(items);
});
\ No newline at end of file
......
......@@ -9,7 +9,13 @@
<button type="submit">submit</button>
</form>
<% items.forEach(function(item){ %>
<div> <p> <%= item %> </p></div>
<form action="/post2" method="post">
<div>
<p> <%= item %> </p>
<input type="hidden" value="<%= item %>" name="value">
<button type="submit">submit</button>
</div>
</form>
<% }); %>
</body>
......