封装一个Express的app.get()
新建一个项目文件,新建/modules/routers.js
const url = require('url');
let methods = {};
let app = function(req,res){
let pathName = url.parse(req.url).pathname;
if(methods[pathName]){
methods[pathName](req,res)
}else{
res.writeHead(404, {
"Content-Type": "text/html;charset=UTF-8"
});
res.end('页面不存在');
}
}
app.get = function(pathName, cd){
methods[pathName] = cd;
}
module.exports = app;
使用上述封装的方法,新建/app.js
const http = require('http');
const app = require('./modules/routers')
http.createServer(app).listen(3000);
app.get('/login',(req,res)=>{
res.writeHead(200, {
"Content-Type": "text/html;charset=UTF-8"
});
res.end('访问登录界面');
})
app.get('/news',(req,res)=>{
res.writeHead(200, {
"Content-Type": "text/html;charset=UTF-8"
});
res.end('访问新闻界面');
})
app.get('/register',(req,res)=>{
res.writeHead(200, {
"Content-Type": "text/html;charset=UTF-8"
});
res.end('访问注册界面');
})
此时浏览器地址栏中输入
http://127.0.0.1:3000/login
页面内容为:访问登录界面
输入
http://127.0.0.1:3000/xxx
页面内容为:页面不存在
封装POST方法
app.js中
const http = require('http');
const path = require('path');
const app = require('./modules/routers')
const ejs = require('ejs');
http.createServer(app).listen(3000);
app.get('/login',(req,res)=>{
ejs.renderFile(path.join(__dirname,'./views/form.ejs'),(err,data)=>{
res.send(data);
})
})
app.post('/doLogin',(req,res)=>{
res.send(req.body);
})
app.get('/news',(req,res)=>{
res.send();
})
app.get('/register',(req,res)=>{
res.send('访问注册界面');
})
/modules/router.js
const fs = require('fs');
const path = require('path');
const url = require('url');
// 封装 res.writeHead和res.end
function changeRes(req, res) {
res['send'] = (str) => {
res.writeHead(200, {
"Content-Type": "text/html;charset=UTF-8"
});
res.end(str);
}
}
let server = () => {
let methods = {
_get: {},
_post: {},
};
let app = function (req, res) {
changeRes(req, res);
let pathName = url.parse(req.url).pathname;
let method = req.method.toLowerCase();
let extname = path.extname(pathName); // 获取后缀名,如果有后缀名走的是静态web,如果没有后缀名就走的是路由
if (!extname) {
switch (method) {
case 'get':
methods["_" + method][pathName](req, res)
break;
case 'post': // 新增封装的post请求
let postData = '';
req.on('data', (chunk) => {
postData += chunk
})
req.on('end', () => {
req.body = postData;
methods["_" + method][pathName](req, res)
})
break;
default:
res.writeHead(404, {
"Content-Type": "text/html;charset=UTF-8"
});
res.end('页面不存在');
break;
}
}
}
app.get = function (pathName, cd) {
methods._get[pathName] = cd;
}
app.post = function (pathName, cd) {
methods._post[pathName] = cd;
}
return app;
}
module.exports = server();
封装访问静态web
app.js
const http = require('http');
const path = require('path');
const app = require('./modules/routers')
const ejs = require('ejs');
http.createServer(app).listen(3000);
app.static('./static'); // 访问静态web文件static
app.get('/login',(req,res)=>{
ejs.renderFile(path.join(__dirname,'./views/form.ejs'),(err,data)=>{
res.send(data);
})
})
app.post('/doLogin',(req,res)=>{
res.send(req.body);
})
app.get('/news',(req,res)=>{
res.send();
})
app.get('/register',(req,res)=>{
res.send('访问注册界面');
})
modules/routers.js中
const fs = require('fs');
const path = require('path');
const url = require('url');
function changeRes(req, res) {
res['send'] = (str) => {
res.writeHead(200, {
"Content-Type": "text/html;charset=UTF-8"
});
res.end(str);
}
}
// 静态web
function getFileMime(extraname) {
let data = fs.readFileSync(path.join(__dirname, '../data/mime.json'));
data = JSON.parse(data.toString())
return data[extraname]
}
// 访问静态web的方法
function initStatic(req, res, staticPath) {
let pathName = req.url; // 获取到请求的url
pathName = url.parse(pathName).pathname; // 使用url内置模块获取路径名
let extname = path.extname(pathName); // 使用内容模块path获取文件后缀名
try {
let data = fs.readFileSync(staticPath + pathName);
if (data) {
let mime = getFileMime(extname); // 使用自定义模块根据后缀名获取Content-Type的值
res.writeHead(200, {
"Content-Type": "" + mime + ";charset=UTF-8"
});
res.end(data);
}
} catch (error) {
}
}
let server = () => {
let methods = {
_get: {},
_post: {},
staticPathName: '../static' // 默认静态文件目录
};
let app = function (req, res) {
changeRes(req, res);
initStatic(req, res, methods.staticPathName); // 执行访问静态web函数
let pathName = url.parse(req.url).pathname;
let method = req.method.toLowerCase();
let extname = path.extname(pathName); // 获取后缀名,如果有后缀名走的是静态web,如果没有后缀名就走的是路由
if (!extname) {
switch (method) {
case 'get':
methods["_" + method][pathName](req, res)
break;
case 'post':
let postData = '';
req.on('data', (chunk) => {
postData += chunk
})
req.on('end', () => {
req.body = postData;
methods["_" + method][pathName](req, res)
})
break;
default:
res.writeHead(404, {
"Content-Type": "text/html;charset=UTF-8"
});
res.end('页面不存在');
break;
}
}
}
app.get = function (pathName, cd) {
methods._get[pathName] = cd;
}
app.post = function (pathName, cd) {
methods._post[pathName] = cd;
}
app.static = function (staticPath) {
methods.staticPathName = staticPath;
}
return app;
}
module.exports = server();
let data = fs.readFileSync(path.join(__dirname, '../data/mime.json'));
引入完整的对应关系需要使用一个json文件:https://github.com/wxyzcctv/node-demo-sd10/blob/main/data/mime.json
该json文件放在/data/mime.json