Dev bootcamp kursumdan bir proje bitiriyorum ve form aracılığıyla html'ye yeni veri gönderme konusunda sorun yaşıyorum. HTML’nin tüm kurulumları var, ama ne zaman yeni veri yayınlarım, hiçbir şey göstermiyor! Ayrıca, sayfanın belirli bir konuya yönlendirilmesini istiyorum.Express.js kullanarak HTML'ye nasıl veri gönderilir?
app.js
app.get('/comments/new', function(req, res){
res.send(fs.readFileSync('./views/comments/newComment.html', 'utf8'));
});
app.post('/topics/:id', function(req, res){
var id = req.params.id;
console.log(req.body);
db.run("INSERT INTO comments(person_created, input) VALUES ('" + req.body.person_created + "','" + req.body.input + "')");
res.redirect("/topics/ " + id)
});
app.get('/topics/:id', function(req, res){
var id = req.params.id;
db.all("SELECT * FROM topics WHERE id = " + id + ";", {}, function(err, topic){
console.log(topic)
var body = topic.body;
db.all("SELECT * FROM comments WHERE topic_id = " + id + ";", {}, function(err, comment){
var person_created = comment.person_created;
var input = comment.input
fs.readFile('./views/topics/show.html', 'utf8', function(err, html){
var renderedHTML = Mustache.render(html, {body:topic, person_created:comment, input:comment});
res.send(renderedHTML);
console.log(comment);
});
});
});
});
schema.js
var sqlite3 = require ('sqlite3');
var db = new sqlite3.Database('./forum.db');
db.serialize(function(){
db.run("CREATE TABLE topics(id integer primary key AUTOINCREMENT, title varchar, creator varchar, date varchar, body varchar);")
db.run("CREATE TABLE comments(person_created varchar, input varchar, topic_id integer, FOREIGN KEY (topic_id) references topics(id));")
db.parallelize(function(){
db.run("INSERT INTO topics(title, creator, date, body) VALUES ('Top R&B Hits of the 80s', 'Michael', '4/15/15', 'Please share some of your favorite R&B Hits from the decade!');")
db.run("INSERT INTO comments(person_created, input, topic_id) VALUES ('Sheila', 'Bille Jean by Michael Jackson', 1);")
db.run("INSERT INTO comments(person_created, input, topic_id) VALUES ('George ', 'Gett Outta of My Dreams by Billy Ocean', 1); ")
});
});
newComment.html
<!DOCTYPE html>
<html lang='en'>
<head>
<style type="text/css">
body{
background-color: gray;
}
</style>
<meta charset='UTF-8'>
<title>Create New Comment</title>
</head>
<body>
<form action="/topics/:id" method="POST">
<center>
<label>
Name:
<br />
<input type="text" name="name" rows="10" cols="50" />
</label>
<label>
<br />
<p></p>
Comment:
<br />
<textarea type="text" name="name" rows="10" cols="50">
</textarea>
</label>
<br />
<button>Submit</button>
</center>
</form>
</body>
</html>
show.html
<!DOCTYPE html>
<html lang='en'>
<head>
<style type="text/css">
body{
background-image: url("http://blog.paradizo.com/wp- content/uploads/2010/03/nyc-empire-room.jpg");
background-position: center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: 100% auto;
}
</style>
<meta charset='UTF-8'>
<title>Topic ID</title>
</head>
<body>
<center>
{{#body}}
<h1>{{body}}</h1>
{{/body}}
<h2>Comments<h2>
<h3>
<ol>
{{#person_created}}
<li>
{{person_created}} - {{input}}
</li>
{{/person_created}}
</ol>
</h3>
<form action="/comments/new" method='GET'>
<button>Create New Comment</button>
</form>
</center>
</body>
</html>
Formları ayrıştırmak için ara katman yazılımı kullanıyor musunuz (ör. 'Body-parser'' urlencoded() 'middleware)? – mscdex
Lütfen kodunuzu sorunuzdaki uygun girinti ile biçimlendirin. Yanlış girinti ile okumak/takip etmek çok kolay değildir. – jfriend00
@ jfriend00 Hangi kodu kullanıyorsunuz? –