diff --git a/tests/tests_hugo/pure_node/index2.html b/tests/tests_hugo/pure_node/index2.html new file mode 100644 index 00000000..18510d78 --- /dev/null +++ b/tests/tests_hugo/pure_node/index2.html @@ -0,0 +1,10 @@ + + + + title + + +

page 2

+ + + diff --git a/tests/tests_hugo/pure_node/myExpress.js b/tests/tests_hugo/pure_node/myExpress.js index 1b151965..af662c4b 100644 --- a/tests/tests_hugo/pure_node/myExpress.js +++ b/tests/tests_hugo/pure_node/myExpress.js @@ -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; + diff --git a/tests/tests_hugo/pure_node/server.js b/tests/tests_hugo/pure_node/server.js index ad6c1b79..45bfec92 100644 --- a/tests/tests_hugo/pure_node/server.js +++ b/tests/tests_hugo/pure_node/server.js @@ -1,82 +1,9 @@ -//const http = require('http') -//const fs = require('fs') -//const url = require('url') - -const sendFile = (file, res) => { - fs.readFile(file, (err, data) => { - if (!err) { - res.writeHead(200, { - 'content-type': 'text/html; charset=utf-8' - }) - res.end(data) - } - else - { - res.writeHead(404) - res.end('file not found') - } - }) -} - -//const server = http.createServer((req, res) => { -// -// let pathName = url.parse(req.url).pathname -// -// if (req.method === "GET") { -// if (pathName === "/") -// sendFile(__dirname + "/index.html", res) -// else -// { -// res.writeHead(404) -// res.end('wrong url') -// } -// } -//}) - -//class myServer { -// constructor() { -// this._gets = {}; -// this._posts = {}; -// this._putss = {}; -// 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); -// } -// else if (req.method === "POST" && this._posts[pathName]) { -// this._posts[pathName](req, res); -// } -// else if (req.method === "PUT" && this._puts[pathName]) { -// this._puts[pathName](req, res); -// } -// else if (req.method === "DELETE" && this._deletes[pathName]) { -// this._deletes[pathName](req, res); -// } else { -// res.writeHead(404) -// res.end('not found'); -// } -// }) -// -// } -// -// get(url, callback) { this._gets[url] = callback; } -// post(url, callback) { this._posts[url] = callback; } -// add(url, callback) { this._puts[url] = callback; } -// delete(url, callback) { this._deletes[url] = callback; } -// -// listen(port, callback) { this._server.listen(port, callback) } -//} -// -//const myExpress = () => { return new myServer() } - - -const myExpress = require('myExpress') +const myExpress = require('./myExpress') const server = myExpress() -server.get("/", (req, res) => sendFile(__dirname + "/index.html", res)) +server.get("/", (req, res) => res.sendFile(__dirname + "/index.html")) +server.get("/index.html", (req, res) => res.sendFile(__dirname + "/index.html")) +server.get("/index2.html", (req, res) => res.sendFile(__dirname + "/index2.html")) const port = 3001; server.listen(port, console.log(`listening on port ${port}`));