三分钟搭建一个 web 服务器
by 草梅友仁
项目地址https://github.com/CaoMeiYouRen/simple-web-server
上面的打不开可以访问这个地址https://gitee.com/caomeiyouren/simple-web-server
使用
新建一个目录,然后切换到该目录
运行以下命令即可,前提是已经安装了 git
1 2 3 4
| git clone https://github.com/CaoMeiYouRen/simple-web-server.git
git clone https://gitee.com/caomeiyouren/simple-web-server.git
|
然后继续运行
运行后可通过 http://127.0.0.1:80 来访问
运行效果
如果不会使用 git 的也可以参考下面的文件目录自己建一个
文件目录
- src/ 源代码
- app.js web 服务脚本
- public/ web 服务器根目录
- package.json npm 包配置文件
package.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| { "name": "simple-web-server", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "server": "node ./src/app.js" }, "author": "CaoMeiYouRen", "license": "MIT", "dependencies": { "express": "^4.16.4" } }
|
app.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| var express = require("express"); var app = express(); var path = require("path");
app.use("/", express.static(path.join(__dirname, "public"))); var http = require("http"); app.get("/", (req, res) => { res.sendFile(__dirname + "/public/index.html"); }); let port = 80; http.createServer(app).listen(port, () => { console.log("HTTP运行端口为 http://127.0.0.1:" + port); });
|
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <!DOCTYPE html> <html lang="zh-CN">
<head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>simple-web-server</title> <style>
</style> <script>
</script> </head>
<body> <h1>HelloWorld</h1> </body>
</html>
|
本文作者:草梅友仁
本文地址: https://blog.cmyr.ltd/archives/fd373c4f.html
版权声明:转载请注明出处!