//myexpress.js
const http = require('http');
const url = require('url');
let routers = [];
class Application {
get(path, hander) {
routers.push({
path,
method: 'get',
hander
});
}
listen2() {
const server = http.createServer(function (req, res) {
const {
pathname
} = url.parse(req.url, true);
var tet = routers.find(v= > {
return v.path == pathname && req.method.toLowerCase() == v.method
})
tet && tet.hander(req, res);
})
// Add the listen method to the Application prototype to match the path and execute the corresponding Handerserver.listen(... arguments)// server.listen(aa,fn)}}module.exports = function () {
return new Application();
}
Copy the code
const express = require('./myexpress.js');
const app = express();
app.get('/'.(req,res) = > {
res.end('Hello world')
})
app.get('/users'.(req,res) = > {
res.end(JSON.stringify({name:'abcooooo'}))
})
app.get('/list'.(req,res) = > {
res.end(JSON.stringify({name:'list'}))
})
app.listen2(3200 , () = > {
console.log('Example listen at 3200')})Copy the code