封裝一個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
頁面內(nèi)容為:訪問登錄界面
輸入
http://127.0.0.1:3000/xxx
頁面內(nèi)容為:頁面不存在
封裝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); // 獲取后綴名华匾,如果有后綴名走的是靜態(tài)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();
封裝訪問靜態(tài)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'); // 訪問靜態(tài)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);
}
}
// 靜態(tài)web
function getFileMime(extraname) {
let data = fs.readFileSync(path.join(__dirname, '../data/mime.json'));
data = JSON.parse(data.toString())
return data[extraname]
}
// 訪問靜態(tài)web的方法
function initStatic(req, res, staticPath) {
let pathName = req.url; // 獲取到請求的url
pathName = url.parse(pathName).pathname; // 使用url內(nèi)置模塊獲取路徑名
let extname = path.extname(pathName); // 使用內(nèi)容模塊path獲取文件后綴名
try {
let data = fs.readFileSync(staticPath + pathName);
if (data) {
let mime = getFileMime(extname); // 使用自定義模塊根據(jù)后綴名獲取Content-Type的值
res.writeHead(200, {
"Content-Type": "" + mime + ";charset=UTF-8"
});
res.end(data);
}
} catch (error) {
}
}
let server = () => {
let methods = {
_get: {},
_post: {},
staticPathName: '../static' // 默認靜態(tài)文件目錄
};
let app = function (req, res) {
changeRes(req, res);
initStatic(req, res, methods.staticPathName); // 執(zhí)行訪問靜態(tài)web函數(shù)
let pathName = url.parse(req.url).pathname;
let method = req.method.toLowerCase();
let extname = path.extname(pathName); // 獲取后綴名,如果有后綴名走的是靜態(tài)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'));
引入完整的對應(yīng)關(guān)系需要使用一個json文件:https://github.com/wxyzcctv/node-demo-sd10/blob/main/data/mime.json
該json文件放在/data/mime.json