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

node 第十天 原生node封装一个简易的服务器

  1. 原生node封装一个简易的服务器, 把前面几天的知识揉和起来做一个服务器
  2. 基础实现, 首页访问, 静态资源服务器, 特定接口封装, 404
  3. app.js 服务器入口文件 app.js node app.js即可启动服务器
const { start } = require('./modules/server');
start();
  1. require_modules.js 整合模块导出
const url = require('url');
const path = require('path');
const http = require('http');
const querystring = require('querystring');
const fs = require('fs');
const multiparty = require('multiparty');module.exports = {url,path,http,querystring,fs,multiparty
};
  1. server.js server 启动模块
const { http } = require('./require_modules');
const { port, host } = require('./config');
const { route } = require('./router');function start() {const server = http.createServer((req, res) => {route(req, res);});server.listen(port, host, () => {console.log(`server listening in http://${host}:${port}`);});
}
module.exports = { start };
  1. router.js 路由模块, 以及接口处理函数对照表
const { url } = require('./require_modules');
const { host, port } = require('./config');
const { staticHanlder, indexHanlder, tableHanlder, notFind } = require('./hanlder');
const hanlder = {'/index': indexHanlder,'/static': staticHanlder,'/index': indexHanlder,'/getTableData': tableHanlder,404: notFind
};
const route = (req, res) => {const thisURL = new URL(`http://${host}:${port}${req.url}`);let pathname = thisURL.pathname;if (pathname === '/') {pathname = '/index/';}const thisHanlder =Object.entries(hanlder).find(([key, val]) => {let reg = new RegExp(`^${key}/.*`);return reg.test(pathname);})?.[1] ?? hanlder[404];thisHanlder(req, res, pathname);
};
module.exports = { route };
  1. hanlder.js 接口处理函数模块
const { fs, path, querystring } = require('../modules/require_modules');
const { getMimeType } = require('../modules/mime_type');
const { root } = require('./config');
const { host, port } = require('./config');function staticHanlder(req, res, pathname) {res.writeHeader(200, { 'content-type': getMimeType(pathname) });const filePath = path.join(root, pathname);fs.stat(filePath, (err, stats) => {if (err) {notFind(req, res, pathname);return;}if (!stats) {notFind(req, res, pathname);return;}if (stats.isDirectory()) {notFind(req, res, pathname);return;}if (stats.isFile()) {fs.readFile(filePath, (err, data) => {if (err) {notFind(req, res, pathname);}res.writeHeader(200, { 'content-type': getMimeType(pathname) });res.end(data);});return;}});
}
function indexHanlder(req, res, pathname) {res.writeHeader(200, { 'content-type': 'text/html;charset=utf-8' });res.end(`<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>index</title></head><body><h1>欢迎~</h1></body></html>`);
}
function tableHanlder(req, res, pathname) {const thisURL = new URL(`http://${host}:${port}${req.url}`);let search = thisURL.search.replace('?', '');const searchInfo = querystring.parse(search);let start = Number(searchInfo.start) || 0;let end = Number(searchInfo.end) || start + 10;const jsonPath = path.join(root, '/data/table.json');fs.readFile(jsonPath, (err, data) => {if (err) {notFind(req, res, pathname);return;}const jsonData = JSON.parse(data.toString('utf-8'));const resData = jsonData.slice(start, end);res.writeHeader(200, { 'content-type': 'application/json;charset=utf-8' });res.end(JSON.stringify(resData));});
}function notFind(req, res, pathname) {res.writeHeader(404, { 'content-type': 'text/html;charset=utf-8' });res.end(`<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>404</title></head><body><h1>not find</h1></body></html>`);
}
function serverError(req, res, pathname) {res.writeHeader(500, { 'content-type': 'text/html;charset=utf-8' });res.end(`<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>500</title></head><body><h1>server error</h1></body></html>`);
}
module.exports = {staticHanlder,indexHanlder,tableHanlder,notFind,serverError
};
  1. mime_type.js 其它模块, 用于获取媒体文件类型
const { path } = require('../modules/require_modules');
const MIME_TYPE = {css: 'text/css',gif: 'image/gif',html: 'text/html',ico: 'image/x-icon',jpeg: 'image/jpeg',jpg: 'image/jpeg',js: 'text/javascript',json: 'application/json',pdf: 'application/pdf',png: 'image/png',svg: 'image/svg+xml',swf: 'application/x-shockwave-flash',tiff: 'image/tiff',txt: 'text/plain',wav: 'audio/x-wav',wma: 'audio/x-ms-wma',wmv: 'video/x-ms-wmv',xml: 'text/xml'
};function getMimeType(pathname) {let ext = path.extname(pathname).replace('.', '').toLowerCase();if (!ext) {ext = pathname;}return MIME_TYPE[ext] || MIME_TYPE['txt'];
}
module.exports = {getMimeType
};
  1. config.js 其它模块 服务器基本信息配置
module.exports = {root: process.cwd(),host: '127.0.0.1',port: '3008'
};
  1. 其实这就是node框架express做的事情 封装服务器有着比较繁琐的过程, 这只是一个简单的模型演示, 比如需要封装上传文件的接口, 你可以用到第三方库multiparty, 需要处理ajax跨域, 你可以封装一个前面学的跨域处理函数 :-)
http://www.lryc.cn/news/204305.html

相关文章:

  • php实战案例记录(25)intval函数的用法
  • laravel框架介绍(二) composer命令下载laravel报错
  • 代码签名证书到期了怎么续费?
  • JAVA 同城服务预约家政小程序开发的优势和运营
  • 基于粒子群算法的无人机航迹规划-附代码
  • 前端使用qrcodejs2插件实现根据网址生成二维码
  • A股风格因子看板 (2023.10 第11期)
  • anaconda安装python 3.11
  • 问题:EventSource 收不到流数据及 EventSource 的 onmessage 方法为null
  • P2 B+树索引
  • 爬虫知识之BeautifulSoup库安装及简单介绍
  • 如何有效取代FTP来帮助企业快速传输大文件
  • 免登陆积分商城原理
  • muduo源码学习base——Atomic(原子操作与原子整数)
  • 最短路相关笔记
  • Web前端-Vue2+Vue3基础入门到实战项目-Day5(自定义指令, 插槽, 案例商品列表, 路由入门)
  • mysql json数据类型 相关函数
  • 如何实现前端实时通信(WebSocket、Socket.io等)?
  • 使用 SSSD 进行网络用户身份验证
  • 紫光展锐携中国联通完成RedCap芯片V517孵化测试
  • 算法通关村第十一关青铜挑战——移位运算详解
  • 2023年面试测试工程师一般问什么问题?
  • 2023年中国汽车覆盖件模具竞争格局、市场规模及行业需求前景[图]
  • vue3项目运行报错import zhCn from “element-plus/lib/locale/lang/zh-cn“
  • 读书笔记:Effective C++ 2.0 版,条款26(歧义)、条款27(禁止部分隐式生成的函数)
  • MySQL基本操作之数据库设计理论
  • SpringBoot的日志系统(日志分组、文件输出、滚动归档)
  • 一种基于HTTPS实现的Web账号登录Linux桌面系统的实现方案
  • 【Linux】psplash制作Linux开机动画
  • WMS透明仓库:实现仓储的全方位可视化与优化