当前位置: 首页 > wzjs >正文

国外建设短视频网站成都建设网官网

国外建设短视频网站,成都建设网官网,济南seo网站建设,wordpress装在xampp向 DeepSeek R1 提问: 我想写一个Web 前端网页,后台用 nodejs js-mdict , 实现在线查询英语单词,并能显示图片 1. 项目结构 首先,创建一个项目目录,结构如下: mydict-app/ ├── public/ │ ├── …

向 DeepSeek R1 提问:
我想写一个Web 前端网页,后台用 nodejs + js-mdict , 实现在线查询英语单词,并能显示图片

1. 项目结构

首先,创建一个项目目录,结构如下:

mydict-app/
├── public/
│   ├── index.html
│   ├── styles.css
│   └── script.js
├── server/
│   └── server.js
├── package.json
└── README.md

英汉词典文件(*.mdx , *.mdd)  已经放在 /js/testdict/ 

2. 安装依赖

在项目根目录下运行以下命令,初始化项目并安装所需的依赖:
cd mydict-app
npm init -y
npm install express
added 69 packages in 6s

npm install js-mdict
added 3 packages in 2s

3. 创建前端页面

在 public/index.html 中创建一个简单的HTML页面,包含一个输入框和三个按钮用于查询单词:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="ie=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>查询英汉词典</title><script src="jquery.js"></script><style>
/* portrait 判断为竖屏 */
@media only screen and (orientation: portrait){#lab1 {display:none;}
} 
/* landscape 判断为横屏 */ 
@media only screen and (orientation: landscape){#lab1 {display: ;}
}    </style>
</head>
<body><form name="form" id="form" action="search" method="GET" target="iframe"><label id="lab1">请输入:</label><input type="text" name="word" id="word" size="30" placeholder="请输入英文单词"><input type="submit" name="eng_han" value="英译汉"><input type="button" name="btn1" id="btn1" value="前缀查询"><input type="button" name="btn2" id="btn2" value="模糊查询"></form><p></p>
<div style="float:left; width:100%;"><div id="result" style="float:left; width:75%; height:500; border:2px;"><iframe name="iframe" id="iframe" width="100%" height="500"> </iframe></div><div id="alist" style="float:right; width:25%; height:500; border:2px;"></div>
</div><script src="script.js"></script>
</body>
</html>

 在 public 中添加一些英汉字典的样式:oalecd8e.css , oalecd8e.js , uk_pron.png, us_pron.png,
copy jquery-3.2.1.min.js pulibc\jquery.js

在 public/script.js 中编写前端逻辑:

  const iframe = $('#iframe')[0]; // 获取 iframe DOM 元素// 页面加载添加:监听iframe网页点击事件$(document).ready(function(){let listener = window.addEventListener('blur', function(){if (document.activeElement === document.getElementById('iframe')){$('iframe').contents().find('a').click(function(event){event.preventDefault();let a = $(this);if (a){let addr = a.attr('href');if (addr.indexOf('entry://')==0 && addr.indexOf('entry://#')!=0){               let word = encodeURIComponent(addr.substring(8));$.ajax({url: `/search?word=${word}`,method: 'GET',success: function (html) {// 将 HTML 内容加载到 iframe 中//$('#iframe').attr('srcdoc', html);let iframeDoc = iframe.contentDocument || iframe.contentWindow.document;if (html){// 写入 HTML 内容iframeDoc.open();iframeDoc.write(html);iframeDoc.close();}},error: function (error) {console.error('entry:请求失败:', error);}});} else if (addr.indexOf('sound://')==0){let url = "/data/" + addr.substring(8);let mp3 = new Audio(url);mp3.addEventListener("canplaythrough", (event)=> {mp3.play();});mp3.addEventListener('error', (e) => {console.error('play error:', e);});} else {console.log('href='+addr);}}});}});});// 前缀查询
$(function(){$("#btn1").click(function(){$.getJSON("/prefix?word="+$("#word").val(), function(data){let items = [];$.each(data, function(i, item){if (i<=20){items[i] = '<a href="/search?word=' +item+ '" target="iframe">' +item+ "</a><br>";}});let a = items.join('\n');if (a) $('#alist').html(a);})})
});// 模糊查询
$(function(){$("#btn2").click(function(){$.getJSON("/fuzzy?word="+$("#word").val(), function(data){let items = [];$.each(data, function(i, item){if (i<=20){items[i] = '<a href="/search?word=' +item+ '" target="iframe">' +item+ "</a><br>";}});let a = items.join('\n');if (a) $('#alist').html(a);})})
});

4. 创建后端服务器

在 server/server2.js 中编写Node.js服务器代码,使用 express 和 js-mdict 来处理查询请求:

const express = require('express');
const fs = require('fs');
const path = require('path');
const Mdict = require('js-mdict');
//console.log(Mdict);
const app = express();
const port = 8002;// 加载MDict词典文件
//const mdict = new Mdict('path/to/your/dictionary.mdx');
const mdx = new Mdict.MDX('/js/testdict/your英汉词典插图版.mdx');
const mdd = new Mdict.MDD('/js/testdict/your英汉词典插图版.mdd');
//console.log(mdd.locate('\\goods'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// 提供静态文件
app.use(express.static(path.join(__dirname, '../public')));const isWord = (txt) => {// 只允许字母、/、空格、-return /^[a-zA-Z \/\-]+$/.test(txt);
};// 处理查询请求
app.get('/search', (req, res) => {let word = req.query.word;if (word) {let data = mdx.lookup(word);console.log("cha: "+ word);if(data.definition){res.send(data.definition);} else {res.status(200).send('this word not found.');}} else {res.status(400).send('error: No word input');}
});// 处理前缀查询请求
app.get('/prefix', (req, res) => {let word = req.query.word;// 检查word是否合法if (word.length>50 || !isWord(word)) {return res.status(400).send('Invalid input.');}if (word) {let alist = mdx.prefix(word);console.log("pre: "+ word);if(alist.length >0){let wordls = [];alist.forEach(function(value){wordls.push(value.keyText);});  res.json(wordls);} else {res.status(400).send('this word not found');}} else {res.status(400).send('error: No word input');}
});// 处理模糊查询请求
app.get('/fuzzy', (req, res) => {let word = req.query.word;// 检查word是否合法if (word.length>50 || !isWord(word)) {return res.status(400).send('Invalid input.');}if (word) {let alist = mdx.fuzzy_search(word,3,1);console.log("fuzzy: "+ word);if(alist.length >0){let wordls = [];alist.forEach(function(value){wordls.push(value.keyText);});  res.json(wordls);} else {res.status(400).send('this word not found');}} else {res.status(400).send('error: No word input');}
});// 指定目录
const dir1 = "/";// 实现image文件下载,不带路径
app.get('/:fileName', (req, res, next) => {let path1 = '/'; let fileName = req.params.fileName; // 捕获文件名// 检查路径中是否包含非法字符(如 ..)if (fileName.includes('..')) {return res.status(400).send('Invalid path: Path traversal is not allowed.');}//console.log(fileName);let extname = path.extname(fileName);let ext = extname.substring(1).toLowerCase();if (['bmp','gif','jpg','png'].includes(ext)){let filePath = path.join(path1, fileName);//console.log(filePath);let data = mdd.locate(filePath);if (data){console.log('key: '+ data.keyText);//console.log(Buffer.isBuffer(data.definition));if (data.definition){let binaryData = Buffer.from(data.definition, 'base64');//res.setHeader('Content-Type', 'application/octet-stream');res.set({'Content-Type': 'image','Content-Disposition': 'attachment;','Content-Length': Buffer.byteLength(binaryData)});//console.log('bytes: '+ Buffer.byteLength(binaryData));res.end(binaryData);} else {res.status(400).send('error: data.definition is null');}} else {res.status(400).send('error: data is null');}} else {res.status(400).send('filename.ext is not image');}
});// 实现image文件下载,*/是路径
app.get('/*/:fileName', (req, res, next) => {let path1 = req.params[0]; // 捕获 * 匹配的部分let fileName = req.params.fileName; // 捕获文件名// 检查路径中是否包含非法字符(如 ..)if (fileName.includes('..')) {return res.status(400).send('Invalid path: Path traversal is not allowed.');}//console.log(fileName);let extname = path.extname(fileName);let ext = extname.substring(1).toLowerCase();if (['bmp','gif','jpg','png'].includes(ext)){let filePath = path.join(dir1,path1, fileName);//console.log(filePath);let data = mdd.locate(filePath);if (data){console.log('key: '+ data.keyText);//console.log(Buffer.isBuffer(data.definition));if (data.definition){let binaryData = Buffer.from(data.definition, 'base64');//res.setHeader('Content-Type', 'application/octet-stream');res.set({'Content-Type': 'image','Content-Disposition': 'attachment;','Content-Length': Buffer.byteLength(binaryData)});//console.log('bytes: '+ Buffer.byteLength(binaryData));res.end(binaryData);} else {res.status(400).send('error: data.definition is null');}} else {res.status(400).send('error: data is null');}} else {res.status(400).send('filename.ext is not image');}
});app.listen(port, () => {console.log(`Server is running on http://localhost:${port}`);
});

5. 运行项目

在项目根目录下运行以下命令启动服务器:cd /js/mydict-app ;
node server/server2.js

然后打开浏览器,访问 http://localhost:8002,你应该可以看到一个简单的词典查询页面。输入单词并点击查询按钮,页面会显示该单词的释义,并显示插图。

6. 部署

你可以将这个应用部署到任何支持 Node.js 的服务器上。

7. 进一步优化

  • 错误处理: 在前端和后端添加更多的错误处理逻辑。

  • UI 改进: 使用前端框架(如React、Vue.js)来改进用户界面。

  • 缓存: 在后端添加缓存机制,提高查询速度。

  • 多词典支持: 支持加载多个词典文件,并提供切换词典的功能。

希望这些步骤能帮助你实现一个简单的在线英语单词查询应用!

http://www.dtcms.com/wzjs/51449.html

相关文章:

  • wordpress自定义末班鸡西seo
  • 越众做的绿色建筑的网站论坛seo设置
  • 学网站开发需要学什么百度快速收录入口
  • 网站建设实训意见建议专业seo外包
  • 罗田做网站网站seo课程
  • c2c模式类型有哪些seo优化网站优化排名
  • 网站怎么做七牛云加速衡水seo排名
  • 响应式网站做seo怎么样百度福州分公司
  • 手机网站建设技术方案电商运营怎么自学
  • 义乌网站建设制作网络营销运营方案
  • 滨湖区知名做网站选哪家天津百度seo
  • 抽奖页面设计模板百度搜索引擎优化指南最新版
  • 企业官网wordpress主题下载aso优化技巧大aso技巧
  • 网站建设公司设计网页的工具西安seo推广优化
  • 上海营销型网站标准广告投放平台公司
  • 用字母做logo的网站北大青鸟软件开发培训学费多少
  • 做网站的经历网店运营工作内容
  • 网站开发锦州seo推广
  • 深圳网站建设服务电话友情链接买卖代理
  • 博客做单页网站竞价推广工作内容
  • 网站正在建设模板线下推广宣传方式有哪些
  • 服务器租用网站自动划分空间正规电商平台有哪些
  • 平果信息网seo修改器
  • 网站产品使用说明书怎么做企业网络营销业务
  • 创业ppt模板免费优化大师破解版app
  • 网站建设需求调研过程百度云搜索入口
  • 做汽车的网站编辑今日最新抗疫数据
  • 单页网站制作 在线 支付培训班该如何建站
  • 网站色彩设计百度网站的网址
  • 怎样优化网站案例域名网站查询