wip nest course
This commit is contained in:
9
tests_hugo/pure_node/index.html
Normal file
9
tests_hugo/pure_node/index.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<!Doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>title</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>hello world</h1>
|
||||
</body>
|
||||
</html>
|
||||
10
tests_hugo/pure_node/index2.html
Normal file
10
tests_hugo/pure_node/index2.html
Normal file
@@ -0,0 +1,10 @@
|
||||
<!Doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>title</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>page 2</h1>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
65
tests_hugo/pure_node/myExpress.js
Normal file
65
tests_hugo/pure_node/myExpress.js
Normal file
@@ -0,0 +1,65 @@
|
||||
const http = require('http')
|
||||
const fs = require('fs')
|
||||
const url = require('url')
|
||||
|
||||
var res = http.ServerResponse.prototype
|
||||
|
||||
res.sendFile = function (file) {
|
||||
fs.readFile(file, (err, data) => {
|
||||
if (!err) {
|
||||
console.log("here")
|
||||
this.writeHead(200, {
|
||||
'content-type': 'text/html; charset=utf-8'
|
||||
})
|
||||
this.end(data)
|
||||
}
|
||||
else
|
||||
{
|
||||
this.writeHead(404)
|
||||
this.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) }
|
||||
}
|
||||
|
||||
const myExpress = () => { return new myServer() }
|
||||
|
||||
module.exports = myExpress;
|
||||
|
||||
10
tests_hugo/pure_node/server.js
Normal file
10
tests_hugo/pure_node/server.js
Normal file
@@ -0,0 +1,10 @@
|
||||
const myExpress = require('./myExpress')
|
||||
const server = myExpress()
|
||||
|
||||
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}`));
|
||||
|
||||
39
tests_hugo/pure_node/server.js.bak
Normal file
39
tests_hugo/pure_node/server.js.bak
Normal file
@@ -0,0 +1,39 @@
|
||||
|
||||
//const https = require('node:https')
|
||||
const http = require('http')
|
||||
const fs = require('fs')
|
||||
const url = require('url')
|
||||
|
||||
const rootName = (name) => {
|
||||
fullName = __dirname + name
|
||||
return fullName
|
||||
}
|
||||
|
||||
const sendHtml = (pathName, response) => {
|
||||
|
||||
console.log(` name: ${pathName}`)
|
||||
console.log(`namePath: ${rootName(pathName)}`)
|
||||
|
||||
fs.readFile(rootName(pathName), (err, data) => {
|
||||
if (err) {
|
||||
response.writeHead(404)
|
||||
response.end('file not found')
|
||||
}
|
||||
else {
|
||||
response.writeHead(200, {
|
||||
'content-type': 'text/html; charset=utf-8'
|
||||
})
|
||||
response.end(data)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const server = http.createServer((request, response) => {
|
||||
let pathName = url.parse(request.url).pathname
|
||||
// read file
|
||||
if (pathName.endsWith(".html"))
|
||||
sendHtml(pathName, response)
|
||||
})
|
||||
|
||||
server.listen('8080')
|
||||
|
||||
9
tests_hugo/with_express/index.html
Normal file
9
tests_hugo/with_express/index.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<!Doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>title</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>hello world</h1>
|
||||
</body>
|
||||
</html>
|
||||
17
tests_hugo/with_express/package.json
Normal file
17
tests_hugo/with_express/package.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "tests_hugo",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "server.js",
|
||||
"dependencies": {
|
||||
"express": "^4.18.2"
|
||||
},
|
||||
"devDependencies": {},
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node server.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC"
|
||||
}
|
||||
10
tests_hugo/with_express/server.js
Normal file
10
tests_hugo/with_express/server.js
Normal file
@@ -0,0 +1,10 @@
|
||||
const express = require('express')
|
||||
const server = express()
|
||||
|
||||
server.get('/', (req, res) => {
|
||||
res.sendFile(__dirname + "/" + "index.html")
|
||||
})
|
||||
|
||||
const port = 3000
|
||||
server.listen(port, console.log(`listening on port ${port} with express`));
|
||||
|
||||
Reference in New Issue
Block a user