nodejs+mongodb+vue前后台配置ueditor的示例代码


Posted in NodeJs onJanuary 02, 2018

笔者在做一个个人博客项目的时候需要一个富文本框输入组件与后台进行交互,但是官方配置里面没有关于nodejs的,于是自己查阅资料研究了一下,最后终于应用到了系统中。

一、后台配置

首先是找到了这个项目:https://github.com/netpi/ueditor,可以通过他开源的代码将ueditor应用的node上面,大概方法如下:

1.先安装依赖:

npm install ueditor --save

2. 配置Node设置

//引入接口文件
const api = require('./api');
//引入文件模块
const fs = require('fs');
//引入处理路径模块
const path = require('path');
//引入处理post数据模块
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) {
  //客户端上传文件设置
  var imgDir = '/img/ueditor/'
  var ActionType = req.query.action;
  if (ActionType === 'uploadimage' || ActionType === 'uploadfile' || ActionType === 'uploadvideo') {
    var file_url = imgDir; //默认图片上传地址
    /*其他上传格式的地址*/
    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');
  }
  // 客户端发起图片列表请求
  else if (req.query.action === 'listimage') {
    var dir_url = imgDir;
    res.ue_list(dir_url); // 客户端会列出 dir_url 目录下的所有图片
  }
  // 客户端发起其它请求
  else {
    // console.log('config.json')
    res.setHeader('Content-Type', 'application/json');
    res.redirect('../nodejs/config.json');
  }
}));

//处理静态文件 todo
// 访问静态资源文件 这里是访问所有dist目录下的静态资源文件
app.use(express.static(path.resolve(__dirname, 'public/')))
app.use('/ueditor', function(req, res) {
  res.render('views/');
});

//监听8888端口
app.listen(8888);
console.log('sucess listen......')

这里需要注意的是因为已经require了ueditor,所以该插件已经安装到了node_module内,所以不需要再拷贝额外的文件了,只需要需要在这个目录下面新建public文件夹存放返回给后台的数据,另外,还需要引入配置文件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/"

nodejs+mongodb+vue前后台配置ueditor的示例代码

然后在组件中配置好就可以了

我的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加载完成后,放入内容。
   });
  },
  methods: {
   getUEContent() { // 获取内容方法
    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环境下可以设置webpack的proxyTable将后端请求代理转发,就可以轻松调试文件上传功能了,同理,vue build之后的文件则需要用Node将静态文件代理到和后端同一个端口上才可以请求后台端口

篇幅有限,文章可能讲述的不太清晰,具体的可以看我这个项目的代码:https://github.com/cheer4chai/myBlog

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

NodeJs 相关文章推荐
PHPStorm 2020.1 调试 Nodejs的多种方法详解
Sep 17 NodeJs
nodejs开发环境配置与使用
Nov 17 NodeJs
nodejs的HTML分析利器node-jquery用法浅析
Nov 08 NodeJs
nodejs连接mongodb数据库实现增删改查
Dec 01 NodeJs
详解Nodejs的timers模块
Dec 22 NodeJs
浅谈Nodejs中的作用域问题
Dec 26 NodeJs
nodeJs链接Mysql做增删改查的简单操作
Feb 04 NodeJs
nodejs个人博客开发第五步 分配数据
Apr 12 NodeJs
Nodejs+express+ejs简单使用实例代码
Sep 18 NodeJs
用Electron写个带界面的nodejs爬虫的实现方法
Jan 29 NodeJs
nodejs log4js 使用详解
May 31 NodeJs
使用nodeJS中的fs模块对文件及目录进行读写,删除,追加,等操作详解
Feb 06 NodeJs
nodejs操作mongodb的填删改查模块的制作及引入实例
Jan 02 #NodeJs
nodejs实现OAuth2.0授权服务认证
Dec 27 #NodeJs
使用nodejs+express实现简单的文件上传功能
Dec 27 #NodeJs
nodejs超出最大的调用栈错误问题
Dec 27 #NodeJs
nodejs实现简单的gulp打包
Dec 21 #NodeJs
nodejs调取微信收货地址的方法
Dec 20 #NodeJs
基于nodejs实现微信支付功能
Dec 20 #NodeJs
You might like
php中使用cookie来保存用户登录信息的实现代码
2012/03/08 PHP
php中++i 与 i++ 的区别
2012/08/08 PHP
Eclipse的PHP插件PHPEclipse安装和使用
2014/07/20 PHP
thinkPHP5.0框架安装教程
2017/03/25 PHP
ThinkPHP中Widget扩展的两种写法及调用方法详解
2017/05/04 PHP
动态加载js的几种方法
2006/10/23 Javascript
谈谈对offsetleft兼容性的理解
2015/11/11 Javascript
js实现(全选)多选按钮的方法【附实例】
2016/03/30 Javascript
Select下拉框模糊查询功能实现代码
2016/07/22 Javascript
基于angular实现模拟微信小程序swiper组件
2017/06/11 Javascript
js es6系列教程 - 新的类语法实战选项卡(详解)
2017/09/02 Javascript
JavaScript原生实现观察者模式的示例
2017/12/15 Javascript
vue 项目接口管理的实现
2019/01/17 Javascript
nodejs文件夹深层复制功能
2019/09/03 NodeJs
微信小程序本地存储实现每日签到、连续签到功能
2019/10/09 Javascript
js判断非127开头的IP地址的实例代码
2020/01/05 Javascript
JS 数组基本用法入门示例解析
2020/01/16 Javascript
JavaScript实现动态留言板
2020/03/16 Javascript
Vue+Java+Base64实现条码解析的示例
2020/09/23 Javascript
Django集成百度富文本编辑器uEditor攻略
2014/07/04 Python
Python字符串格式化
2015/06/15 Python
python如何通过protobuf实现rpc
2016/03/06 Python
取numpy数组的某几行某几列方法
2018/04/03 Python
flask框架使用orm连接数据库的方法示例
2018/07/16 Python
Python使用jsonpath-rw模块处理Json对象操作示例
2018/07/31 Python
简单瞅瞅Python vars()内置函数的实现
2019/09/27 Python
Python 转换RGB颜色值的示例代码
2019/10/13 Python
html5摇一摇代码优化包括DeviceMotionEvent等等
2014/09/01 HTML / CSS
365 Tickets英国:全球景点门票
2019/07/06 全球购物
世界上最大的皮肤科医生拥有和经营的美容网站:LovelySkin
2021/01/03 全球购物
大学生应聘推荐信范文
2013/11/19 职场文书
企业管理部经理岗位职责
2013/12/24 职场文书
我的梦想演讲稿
2014/04/30 职场文书
春节联欢会策划方案
2014/05/16 职场文书
2015年世界艾滋病日活动总结
2015/03/24 职场文书
tensorflow中的数据类型dtype用法说明
2021/05/26 Python