nodejs+mongodb+vue前后臺配置ueditor的示例代碼
筆者在做一個個人博客項(xiàng)目的時候需要一個富文本框輸入組件與后臺進(jìn)行交互,但是官方配置里面沒有關(guān)于nodejs的,于是自己查閱資料研究了一下,最后終于應(yīng)用到了系統(tǒng)中。
一、后臺配置
首先是找到了這個項(xiàng)目:https://github.com/netpi/ueditor,可以通過他開源的代碼將ueditor應(yīng)用的node上面,大概方法如下:
1.先安裝依賴:
npm install ueditor --save
2. 配置Node設(shè)置
//引入接口文件
const api = require('./api');
//引入文件模塊
const fs = require('fs');
//引入處理路徑模塊
const path = require('path');
//引入處理post數(shù)據(jù)模塊
var bodyParser = require('body-parser');
//引入express
const express = require('express');
const app = express();
//引入ueditor
const ueditor = require("ueditor")
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
//更改限定大小
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
// parse application/json
app.use(bodyParser.json())
app.use(api)
app.use("/ueditor/ue", ueditor(path.join(__dirname, 'public'), function(req, res, next) {
//客戶端上傳文件設(shè)置
var imgDir = '/img/ueditor/'
var ActionType = req.query.action;
if (ActionType === 'uploadimage' || ActionType === 'uploadfile' || ActionType === 'uploadvideo') {
var file_url = imgDir; //默認(rèn)圖片上傳地址
/*其他上傳格式的地址*/
if (ActionType === 'uploadfile') {
file_url = '/file/ueditor/'; //附件
}
if (ActionType === 'uploadvideo') {
file_url = '/video/ueditor/'; //視頻
}
res.ue_up(file_url); //你只要輸入要保存的地址 。保存操作交給ueditor來做
res.setHeader('Content-Type', 'text/html');
}
// 客戶端發(fā)起圖片列表請求
else if (req.query.action === 'listimage') {
var dir_url = imgDir;
res.ue_list(dir_url); // 客戶端會列出 dir_url 目錄下的所有圖片
}
// 客戶端發(fā)起其它請求
else {
// console.log('config.json')
res.setHeader('Content-Type', 'application/json');
res.redirect('../nodejs/config.json');
}
}));
//處理靜態(tài)文件 todo
// 訪問靜態(tài)資源文件 這里是訪問所有dist目錄下的靜態(tài)資源文件
app.use(express.static(path.resolve(__dirname, 'public/')))
app.use('/ueditor', function(req, res) {
res.render('views/');
});
//監(jiān)聽8888端口
app.listen(8888);
console.log('sucess listen......')
這里需要注意的是因?yàn)橐呀?jīng)require了ueditor,所以該插件已經(jīng)安裝到了node_module內(nèi),所以不需要再拷貝額外的文件了,只需要需要在這個目錄下面新建public文件夾存放返回給后臺的數(shù)據(jù),另外,還需要引入配置文件config.json
二、前臺配置
vue的前臺配置需要下載ueditor的文件放在目錄中,我將其放在了static文件夾中,在vue入口文件中引入ueditor的文件:
import '../static/UE/ueditor.config.js' import '../static/UE/ueditor.all.min.js' import '../static/UE/lang/zh-cn/zh-cn.js' import '../static/UE/ueditor.parse.min.js'
值得一提的是需要將ueditor.config.js文件中的目錄配置為放置該插件的目錄:
window.UEDITOR_HOME_URL = "/static/UE/"

然后在組件中配置好就可以了
我的UE.vue組件:
<template>
<script :id=id type="text/plain"></script>
</template>
<script>
export default {
name: 'UE',
data () {
return {
editor: null
}
},
props: {
defaultMsg: {
type: String
},
config: {
type: Object
},
id: {
type: String
},
},
mounted() {
const _this = this;
this.editor = UE.getEditor(this.id, this.config); // 初始化UE
this.editor.addListener("ready", function () {
_this.editor.setContent(_this.defaultMsg); // 確保UE加載完成后,放入內(nèi)容。
});
},
methods: {
getUEContent() { // 獲取內(nèi)容方法
return this.editor.getContent()
}
},
destroyed() {
this.editor.destroy();
}
}
</script>
引入方式:
<UE :defaultMsg=defaultMsg :config=config :id=ue1 ref="ue"></UE>
data() {
return {
defaultMsg: "",
image: "",
config: {
initialFrameWidth: null,
initialFrameHeight: 350
},
ue1: "ue1"
};
},
就可以成功配置好ueditor的基本功能了
三、前后臺請求代理
在vue dev環(huán)境下可以設(shè)置webpack的proxyTable將后端請求代理轉(zhuǎn)發(fā),就可以輕松調(diào)試文件上傳功能了,同理,vue build之后的文件則需要用Node將靜態(tài)文件代理到和后端同一個端口上才可以請求后臺端口
篇幅有限,文章可能講述的不太清晰,具體的可以看我這個項(xiàng)目的代碼:https://github.com/cheer4chai/myBlog
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 利用Vue.js+Node.js+MongoDB實(shí)現(xiàn)一個博客系統(tǒng)(附源碼)
- node.js操作mongoDB數(shù)據(jù)庫示例分享
- node.js連接mongoDB數(shù)據(jù)庫 快速搭建自己的web服務(wù)
- Node.js中使用mongoskin操作mongoDB實(shí)例
- nodejs連接mongodb數(shù)據(jù)庫實(shí)現(xiàn)增刪改查
- AngularJS + Node.js + MongoDB開發(fā)的基于高德地圖位置的通訊錄
- Node.js對MongoDB數(shù)據(jù)庫實(shí)現(xiàn)模糊查詢的方法
- Vue+Node實(shí)現(xiàn)商品列表的分頁、排序、篩選,添加購物車功能詳解
- node和vue實(shí)現(xiàn)商城用戶地址模塊
- nodejs(officegen)+vue(axios)在客戶端導(dǎo)出word文檔的方法
- Vue+Node服務(wù)器查詢Mongo數(shù)據(jù)庫及頁面數(shù)據(jù)傳遞操作實(shí)例分析
相關(guān)文章
Nodejs?http模塊返回內(nèi)容中文亂碼問題及解決
這篇文章主要介紹了Nodejs?http模塊返回內(nèi)容中文亂碼問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
node.js中path路徑模塊的使用方法實(shí)例分析
這篇文章主要介紹了node.js中path路徑模塊的使用方法,結(jié)合實(shí)例形式分析了node.js path路徑模塊的基本功能、原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下2020-02-02
node.js使用express-jwt報錯:expressJWT?is?not?a?function解決
這篇文章主要給大家介紹了關(guān)于node.js使用express-jwt報錯:expressJWT?is?not?a?function解決的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-03-03
Nodejs Buffer的使用及Stream流和事件機(jī)制詳解
這篇文章主要為大家介紹了Nodejs Buffer的使用及Stream流和事件機(jī)制詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
利用n 升級工具升級Node.js版本及在mac環(huán)境下的坑
這篇文章主要介紹了利用n 升級工具升級Node.js的方法,以及通過網(wǎng)友的測試發(fā)現(xiàn)在mac環(huán)境下利用n工具升級不成功導(dǎo)致node.js不可用的解決方法,有需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02

