express like code is now working with require and sendfile
This commit is contained in:
@@ -2,22 +2,26 @@ const http = require('http')
|
||||
const fs = require('fs')
|
||||
const url = require('url')
|
||||
|
||||
const sendFile = (file, res) => {
|
||||
var res = http.ServerResponse.prototype
|
||||
|
||||
res.sendFile = function (file) {
|
||||
fs.readFile(file, (err, data) => {
|
||||
if (!err) {
|
||||
res.writeHead(200, {
|
||||
console.log("here")
|
||||
this.writeHead(200, {
|
||||
'content-type': 'text/html; charset=utf-8'
|
||||
})
|
||||
res.end(data)
|
||||
this.end(data)
|
||||
}
|
||||
else
|
||||
{
|
||||
res.writeHead(404)
|
||||
res.end('file not found')
|
||||
this.writeHead(404)
|
||||
this.end('file not found')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
class myServer {
|
||||
constructor() {
|
||||
this._gets = {};
|
||||
@@ -26,8 +30,9 @@ class myServer {
|
||||
this._deletes = {};
|
||||
|
||||
this._server = http.createServer((req, res) => {
|
||||
|
||||
const pathName = url.parse(req.url).pathname;
|
||||
|
||||
|
||||
if (req.method === "GET" && this._gets[pathName]) {
|
||||
this._gets[pathName](req, res);
|
||||
}
|
||||
@@ -43,8 +48,7 @@ class myServer {
|
||||
res.writeHead(404)
|
||||
res.end('not found');
|
||||
}
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
get(url, callback) { this._gets[url] = callback; }
|
||||
@@ -55,4 +59,7 @@ class myServer {
|
||||
listen(port, callback) { this._server.listen(port, callback) }
|
||||
}
|
||||
|
||||
const myExpress = () => { return new myServer() }
|
||||
|
||||
module.exports = myExpress;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user