Skip to content

Commit

Permalink
webserver
Browse files Browse the repository at this point in the history
  • Loading branch information
lefex committed Oct 14, 2020
1 parent 675f033 commit 634c6f8
Show file tree
Hide file tree
Showing 12 changed files with 11,348 additions and 0 deletions.
Binary file modified .DS_Store
Binary file not shown.
28 changes: 28 additions & 0 deletions demo/rightLimit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>禁止点击右键</title>
</head>

<body>
<script>
// 禁止使用 F12
window.onkeydown = window.onkeyup = window.onkeypress = function (event) {
// F12 键码为 123
if (event.keyCode === 123) {
event.preventDefault();
window.event.returnValue = false;
}
}
// 为右键添加自定义事件,可以禁用
window.oncontextmenu = function () {
event.preventDefault();
return false;
}
</script>
</body>

</html>
3 changes: 3 additions & 0 deletions webserver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a websever

https://github.com/koajs/koa
76 changes: 76 additions & 0 deletions webserver/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* @file app.js
* @author 公众号:素燕
* @description https://lefex.github.io/fe-mini-course/
*/

const Express = require('express');
const app = new Express();
const fs = require('fs');
const path = require('path');
const os = require('os');

const port = 2081;

const generateHtml = (content) => {
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>欢迎来到《前端小课》</h1>
<div>${content}</div>
</body>
</html>`;
};


const ipAddress = () => {
var interfaces = os.networkInterfaces();
for (var devName in interfaces) {
var iface = interfaces[devName];
for (var i = 0; i < iface.length; i++) {
var alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
return alias.address;
}
}
}
}

app.on('error', err => {
log.error('server error', err)
});

// root
app.get('/', (req, res) => {
res.send(generateHtml(`the right address is like: http://localhost:${port}/index.html`));
});

// get html file
app.get('/*.html', (req, res) => {
console.log(`receive a request: ${req.originalUrl}`);
let des = path.resolve(__dirname, './dist');
let filePath = des + req.originalUrl;
let resHtml;
console.log(`begin find file in: ${filePath}`);
if (fs.existsSync(filePath)) {
resHtml = fs.readFileSync(filePath, 'utf-8');
}
else {
console.log(`file not in path: ${filePath}`);
resHtml = generateHtml(`${req.originalUrl} file not exist, please create file in ${des}`);
}
res.send(resHtml)
});

app.listen(port);

console.log(`Project is running at: http://localhost:${port}`);
const appHost = ipAddress();
if (appHost) {
console.log(`Project is running at: http://${appHost}:${port}`);
}
12 changes: 12 additions & 0 deletions webserver/dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>欢迎来到《前端小课》</h1>
<div>搭建一个万能的 webserver</div>
</body>
</html>
Loading

0 comments on commit 634c6f8

Please sign in to comment.