From 6faad877344bf876a16ef017f26ccd85951b5cdf Mon Sep 17 00:00:00 2001 From: hugo gogo Date: Wed, 19 Oct 2022 00:51:52 +0200 Subject: [PATCH] trying to export myexpress and failed make sendfile a method of res --- tests/tests_hugo/pure_node/myExpress.js | 58 +++++++++++++++++ tests/tests_hugo/pure_node/server.js | 87 +++++++++++++------------ 2 files changed, 103 insertions(+), 42 deletions(-) create mode 100644 tests/tests_hugo/pure_node/myExpress.js diff --git a/tests/tests_hugo/pure_node/myExpress.js b/tests/tests_hugo/pure_node/myExpress.js new file mode 100644 index 00000000..1b151965 --- /dev/null +++ b/tests/tests_hugo/pure_node/myExpress.js @@ -0,0 +1,58 @@ +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') + } + }) +} + +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) } +} + +module.exports = myExpress; diff --git a/tests/tests_hugo/pure_node/server.js b/tests/tests_hugo/pure_node/server.js index d1787344..ad6c1b79 100644 --- a/tests/tests_hugo/pure_node/server.js +++ b/tests/tests_hugo/pure_node/server.js @@ -1,7 +1,6 @@ - -const http = require('http') -const fs = require('fs') -const url = require('url') +//const http = require('http') +//const fs = require('fs') +//const url = require('url') const sendFile = (file, res) => { fs.readFile(file, (err, data) => { @@ -34,47 +33,51 @@ const sendFile = (file, res) => { // } //}) -class customServer { - 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._gets[pathName](req, res); - } - else if (req.method === "PUT" && this._puts[pathName]) { - this._gets[pathName](req, res); - } - else if (req.method === "DELETE" && this._deletes[pathName]) { - this._gets[pathName](req, res); - } else { - res.writeHead(404) - res.end('not found'); - } - }) - - } +//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() } - 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 server = new customServer(); -server.get("/", (req, res) => sendFile(__dirname + "/index.html", res)); +const myExpress = require('myExpress') +const server = myExpress() + +server.get("/", (req, res) => sendFile(__dirname + "/index.html", res)) const port = 3001; -//server.listen(port); server.listen(port, console.log(`listening on port ${port}`));