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

news/2025/3/21 6:26:55/
  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.ppmy.cn/news/1167272.html

相关文章

系统架构设计之微内核架构(Microkernel Architecture)

微内核架构&#xff08;Microkernel Architecture&#xff09; 一. 什么是微内核架构二. 微内核架构风格-拓扑结构三. 微内核的核心系统设计的三个关键点3.1 插件管理3.2 插件连接3.3 插件通信 四. 微内核架构的优缺点 一. 什么是微内核架构 微内核架构是一种面向功能进行拆分的…

量子信息处理器可能能够提供高度压缩的生成对抗学习任务的版本

量子信息处理在生成对抗学习任务中的应用可能性&#xff0c;以及量子信息处理器在表示高维向量和执行线性代数运算上的优势。 举个例子 假设底层数据由M个在N维实数或复数空间中的归一化向量~vj组成&#xff0c;使得数据的&#xff08;归一化&#xff09;协方差矩阵为C (1/M…

shell_36.Linux处理循环的输出

处理循环的输出 1.在 shell 脚本中&#xff0c;可以对循环的输出使用管道或进行重定向。 这可以通过在 done 命令之后添加一个处理命令来实现&#xff1a; for file in /home/rich/* do if [ -d "$file" ] then echo "$file is a directory" elif echo &…

stable diffusion如何解决gradio外链无法开启的问题

问题确认 为了确认gradio开启不了是gradio库的问题还是stable diffusion的问题&#xff0c;可以先执行这样一段demo代码 import gradio as grdef greet(name):return "Hello " name "!"demo gr.Interface(fngreet, inputs"text", outputs&q…

v-model和.sync区别

在vue2中提供了.sync修饰符&#xff0c;但是在vue3中不再支持.sync&#xff0c;取而代之的是v-model。 1.在vue2中v-model和.sync区别&#xff1a; 1.相同点&#xff1a;都是语法糖&#xff0c;都可以实现父子组件中的数据的双向通信。 ​ 区别在于往回传值的时候. sync 的 $…

【SA8295P 源码分析 (四)】19 - QNX Host NFS 文件系统配置

【SA8295P 源码分析】19 - QNX Host NFS 文件系统配置 一、NFS Server二、NFS Client三、NFS 相关的文件及目录四、将文件放入QNX 文件系统中五、编译下载验证系列文章汇总见:《【SA8295P 源码分析 (四)】网络模块 文章链接汇总 - 持续更新中》 本文链接:《【SA8295P 源码分析…

UE4 距离场

在项目设置的渲染模块可打开距离场 把该节点连上&#xff0c;该节点的意思是&#xff0c;距离表面越近&#xff0c;材质显示值为0 不接近表面时&#xff1a; 接近表面时 可勾选该值即可看到距离场具体效果&#xff1a; 未接触表面时&#xff1a; 接触表面时&#xff1a; 产生…

华为OD机试 - TLV格式 - 逻辑分析(Java 2023 B卷 100分)

目录 专栏导读一、题目描述二、输入描述三、输出描述四、解题思路五、Java算法源码六、效果展示1、输入2、输出3、说明 华为OD机试 2023B卷题库疯狂收录中&#xff0c;刷题点这里 专栏导读 本专栏收录于《华为OD机试&#xff08;JAVA&#xff09;真题&#xff08;A卷B卷&#…