我对Nodejs(和编码,一般来说)是新手。我正在尝试构建一个小型内容管理系统。我希望我的潜在最终用户能够设计他们的内容(粗体、斜体、超链接等)在点击按钮将帖子提交到我的博客之前。我在工具栏上看到的那种
以下是我在阅读了奎尔的指南之后得到的信息:
类型ejs公司
<style>
#editor {
height: 300px;
}
</style>
<!-- Include Quill stylesheet -->
<link href="https://cdn.quilljs.com/1.0.0/quill.snow.css" rel="stylesheet">
<!-- Create the toolbar container -->
<div id="toolbar">
<button class="ql-bold">Bold</button>
<button class="ql-italic">Italic</button>
</div>
<form action = '/blog' method = 'POST'>
<!-- Create the editor container -->
<input type = 'text' name = 'title' placeholder = 'title'>
<label for="body">About me</label>
<input name="body" type="hidden">
<div id="editor">
<p>I am at my wit's end. Stackoverflow is my last hope</p>
</div>
<button type="submit">Submit</button>
</form>
<!-- Include the Quill library -->
<script src="https://cdn.quilljs.com/1.0.0/quill.js"></script>
<!-- Initialize Quill editor -->
<script>
var editor = new Quill('#editor', {
modules: { toolbar: '#toolbar' },
theme: 'snow'
});
var form = document.querySelector('form');
form.onsubmit = function() {
console.log("hey")
// Populate hidden form on submit
var body = document.querySelector('input[name=body]');
body.value = JSON.stringify(quill.getContents());
console.log("Submitted", $(form).serialize(), $(form).serializeArray());
};
</script>
应用程序。js文件
var app = require('express')(),
mongoose = require('mongoose'),
Blog = require('./model/blog'),
bodyParser = require('body-parser');
mongoose.connect("mongodb://localhost/quill_demo", {useMongoClient:true});
app.set('view engine','ejs'); //EJS WILL BE THE DEFAULT FILE THAT DISPLAYS THE FRONT-END DATA
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true})); // retrieve text from submitted forms from the EJS files.
//Add new blog to the database
app.post('/blog', function(req, res){
var myPost = {
title:req.body.title,
body: req.body.body
};
Blog.create(myPost, function(err, newBlog){
if(err){
console.log(err);
} else {
console.log("New Blog has been added");
newBlog.save(function(err, savedBlog){
if(err){
console.log(err);
}else {
console.log(savedBlog);
res.redirect('/blog');
}
});
}
});
});
我在点击提交后检查数据库。“标题”字段工作正常。但在“body”字段中没有添加任何内容。
所以,看起来我少了几块。如果有任何帮助,我将不胜感激。如果有人简单地在我的代码中添加缺失的部分,我不会介意,稍后我将从他们的步骤中学习。
P、 我没有CS学位。我自己学习。所以,尽量用外行的话回答