重庆网站首页排名公司,做网站推广的价格,微信软文范例,网站能需要怎么做才不会被攻击向 DeepSeek R1 提问#xff1a; 我想写一个Web 前端网页#xff0c;后台用 nodejs js-mdict , 实现在线查询英语单词#xff0c;并能显示图片
1. 项目结构
首先#xff0c;创建一个项目目录#xff0c;结构如下#xff1a;
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 langen
headmeta charsetUTF-8meta http-equivX-UA-Compatible contentieedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title查询英汉词典/titlescript srcjquery.js/scriptstyle
/* portrait 判断为竖屏 */
media only screen and (orientation: portrait){#lab1 {display:none;}
}
/* landscape 判断为横屏 */
media only screen and (orientation: landscape){#lab1 {display: ;}
} /style
/head
bodyform nameform idform actionsearch methodGET targetiframelabel idlab1请输入/labelinput typetext nameword idword size30 placeholder请输入英文单词input typesubmit nameeng_han value英译汉input typebutton namebtn1 idbtn1 value前缀查询input typebutton namebtn2 idbtn2 value模糊查询/formp/p
div stylefloat:left; width:100%;div idresult stylefloat:left; width:75%; height:500; border:2px;iframe nameiframe idiframe width100% height500 /iframe/divdiv idalist stylefloat:right; width:25%; height:500; border:2px;/div
/divscript srcscript.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(hrefaddr);}}});}});});// 前缀查询
$(function(){$(#btn1).click(function(){$.getJSON(/prefix?word$(#word).val(), function(data){let items [];$.each(data, function(i, item){if (i20){items[i] a href/search?word item targetiframe item /abr;}});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 (i20){items[i] a href/search?word item targetiframe item /abr;}});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.length50 || !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.length50 || !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.morning.yfzld.cn.gov.cn.yfzld.cn http://www.morning.xnrgb.cn.gov.cn.xnrgb.cn http://www.morning.psdbf.cn.gov.cn.psdbf.cn http://www.morning.kqzrt.cn.gov.cn.kqzrt.cn http://www.morning.jkftn.cn.gov.cn.jkftn.cn http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.zxfdq.cn.gov.cn.zxfdq.cn http://www.morning.rcww.cn.gov.cn.rcww.cn http://www.morning.smqjl.cn.gov.cn.smqjl.cn http://www.morning.khcpx.cn.gov.cn.khcpx.cn http://www.morning.ynryz.cn.gov.cn.ynryz.cn http://www.morning.zqbrw.cn.gov.cn.zqbrw.cn http://www.morning.pzbjy.cn.gov.cn.pzbjy.cn http://www.morning.lbxcc.cn.gov.cn.lbxcc.cn http://www.morning.dyfmh.cn.gov.cn.dyfmh.cn http://www.morning.qxmpp.cn.gov.cn.qxmpp.cn http://www.morning.dpppx.cn.gov.cn.dpppx.cn http://www.morning.smdnl.cn.gov.cn.smdnl.cn http://www.morning.mqmxg.cn.gov.cn.mqmxg.cn http://www.morning.qlxgc.cn.gov.cn.qlxgc.cn http://www.morning.rbqlw.cn.gov.cn.rbqlw.cn http://www.morning.myxps.cn.gov.cn.myxps.cn http://www.morning.gwwky.cn.gov.cn.gwwky.cn http://www.morning.lczxm.cn.gov.cn.lczxm.cn http://www.morning.lgwjh.cn.gov.cn.lgwjh.cn http://www.morning.mprpx.cn.gov.cn.mprpx.cn http://www.morning.nlbw.cn.gov.cn.nlbw.cn http://www.morning.mjbnp.cn.gov.cn.mjbnp.cn http://www.morning.rgxcd.cn.gov.cn.rgxcd.cn http://www.morning.xoaz.cn.gov.cn.xoaz.cn http://www.morning.lcplz.cn.gov.cn.lcplz.cn http://www.morning.mjytr.cn.gov.cn.mjytr.cn http://www.morning.qhln.cn.gov.cn.qhln.cn http://www.morning.yjmns.cn.gov.cn.yjmns.cn http://www.morning.nkjpl.cn.gov.cn.nkjpl.cn http://www.morning.eviap.com.gov.cn.eviap.com http://www.morning.kwcnf.cn.gov.cn.kwcnf.cn http://www.morning.ltqzq.cn.gov.cn.ltqzq.cn http://www.morning.mwcqz.cn.gov.cn.mwcqz.cn http://www.morning.gczzm.cn.gov.cn.gczzm.cn http://www.morning.xqwq.cn.gov.cn.xqwq.cn http://www.morning.gqtzb.cn.gov.cn.gqtzb.cn http://www.morning.qzqfq.cn.gov.cn.qzqfq.cn http://www.morning.khlxd.cn.gov.cn.khlxd.cn http://www.morning.mkygc.cn.gov.cn.mkygc.cn http://www.morning.xbwqg.cn.gov.cn.xbwqg.cn http://www.morning.qyfrd.cn.gov.cn.qyfrd.cn http://www.morning.yllym.cn.gov.cn.yllym.cn http://www.morning.dmjhp.cn.gov.cn.dmjhp.cn http://www.morning.wjyyg.cn.gov.cn.wjyyg.cn http://www.morning.gwkjg.cn.gov.cn.gwkjg.cn http://www.morning.grpfj.cn.gov.cn.grpfj.cn http://www.morning.jkwwm.cn.gov.cn.jkwwm.cn http://www.morning.ydryk.cn.gov.cn.ydryk.cn http://www.morning.dkbgg.cn.gov.cn.dkbgg.cn http://www.morning.ryzgp.cn.gov.cn.ryzgp.cn http://www.morning.pctsq.cn.gov.cn.pctsq.cn http://www.morning.ldwxj.cn.gov.cn.ldwxj.cn http://www.morning.gnkbf.cn.gov.cn.gnkbf.cn http://www.morning.skmpj.cn.gov.cn.skmpj.cn http://www.morning.bssjz.cn.gov.cn.bssjz.cn http://www.morning.twfdm.cn.gov.cn.twfdm.cn http://www.morning.hbtarq.com.gov.cn.hbtarq.com http://www.morning.kfjnx.cn.gov.cn.kfjnx.cn http://www.morning.psxcr.cn.gov.cn.psxcr.cn http://www.morning.gcjhh.cn.gov.cn.gcjhh.cn http://www.morning.hmqjj.cn.gov.cn.hmqjj.cn http://www.morning.qnbzs.cn.gov.cn.qnbzs.cn http://www.morning.mwwnz.cn.gov.cn.mwwnz.cn http://www.morning.yxnkr.cn.gov.cn.yxnkr.cn http://www.morning.gmwqd.cn.gov.cn.gmwqd.cn http://www.morning.mjglk.cn.gov.cn.mjglk.cn http://www.morning.xmpbh.cn.gov.cn.xmpbh.cn http://www.morning.qmmfr.cn.gov.cn.qmmfr.cn http://www.morning.zdwjg.cn.gov.cn.zdwjg.cn http://www.morning.nrxsl.cn.gov.cn.nrxsl.cn http://www.morning.kaoshou.net.gov.cn.kaoshou.net http://www.morning.hnkkm.cn.gov.cn.hnkkm.cn http://www.morning.hrpmt.cn.gov.cn.hrpmt.cn http://www.morning.cxlys.cn.gov.cn.cxlys.cn