如何使用 Express.js 将数据发布到 HTML
How to post data to HTML using Express.js?
我正在完成开发训练营课程中的一个项目,但我无法通过表单将新数据发布到我的 html。我已经设置了所有 HTML,但是每当我发布新数据时,什么都没有显示!我还希望将页面重定向到特定主题。
应用.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);
});
});
});
});
架构.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); ")
});
});
新评论.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>
显示.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>
您的表单操作不应为/topics/:id,因为 :id 是由 express 计算的参数值,因此您将传入实际值。
因此,如果您的表单操作是/topics/505,则 505 将是 req.params.id 的值
您正在将POST
数据与表单一起发送到 /topics/:id
上POST
路由器的/topics/:id
。您应该POST
到具有要用于路由器中逻辑的 ID 的 URL。然后,可通过 req.params.id
提供此 ID。
使用表单呈现页面时,可以使用 res.locals
将当前 ID 作为局部变量传递,然后使用此动态局部变量构造表单action
属性。然后,这将通过 req.params
将正确的 ID 传递给您的路由器。
作为旁注,您正在尝试访问person_created
和input
req.body
,但表单中的输入只能通过其name
输入值获得。您的name
值当前均为name
。您应该将它们命名uniquely
并确保已启用body-parser
中间件。
相关文章:
- 在页面呈现之前更改HTML数据
- 当使用ajax并将html数据保存为对象时,收听浏览器返回按钮.好的或坏的
- 使用数据属性将HTML数据复制到另一个元素
- 使用jQuery在select选项上设置HTML数据属性
- 使用Javascript而不是html数据属性配置Parsley
- 访问HTML数据集
- 如何调用更改事件,例如在 HTML 数据列表上选择
- 检测 点击 在 ANDROID 中使用 HTML 数据查看
- 选择具有特定值的 html 数据属性
- jQuery的HTML数据效果
- 生成html时,在html数据属性中存储由.push()填充的数组
- Powerbuilder/Javascript HTML数据窗口SetItem日期时间失败
- html数据属性在firefox中使用javascript时没有按预期显示,但在chrome中显示正确
- 由于数据中的 & 符号,无法在 C# 上传递整个 html 数据
- 内联 HTML 数据角色<>脚本 jquery:Kendo Widget Initialization
- 如何将带有普通对象集合的 Js 数组对象传递给 ajax post'html' 数据类型中的控制器
- 使用 PrototypeJS 1.6 访问锚点上的 HTML 数据属性
- 如何更改 HTML 数据列表输入上显示的图标?隐藏是可能的,但我可以将其更改为其他一些图标,例如向下箭头
- Android - 如何在运行时生成的HTML数据中加载外部javascript文件
- 从 html 数据设置 jquery 延迟