1 Star 0 Fork 0

草梅友仁 / 简单网站服务器

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

simple-web-server/简单网站服务器

by草梅友仁

使用方法

npm i #下载依赖
npm run server  #运行 即node ./src/app.js 也可以到src目录下再运行 node app.js

运行后可通过 http://127.0.0.1:80 来访问

文件源码

app.js

var express = require("express");
var app = express();//使用express框架
var path = require("path");//这个是node.js自带的路径处理模块
/** 
 * app.use("/", express.static(path.join(__dirname, "public")));
 * app.use("访问路径",express.static("要公开的目录"))
 * 下面这段的意思是通过 域名/xxx 能够访问到 public/xxx 的内容
 * 即将public目录作为静态目录公开
 * path.join(__dirname, "public")能够获取到public的绝对目录
 * 使用绝对目录作为根域名有一个好处就是能够保证找到这个目录,相对目录可能会出错
*/
app.use("/", express.static(path.join(__dirname, "public")));
var http = require("http");//导入http模块
app.get("/", (req, res) => {//当以get方式访问根目录时,返回index.html
    res.sendFile(__dirname + "/public/index.html");
});
let port = 80;//脚本运行端口为80
http.createServer(app).listen(port, () => {
    console.log("HTTP运行端口为 http://127.0.0.1:" + port);
});

index.html

<!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>

Comments ( 0 )

Sign in for post a comment

About

simple-web-server/简单网站服务器 spread retract
Cancel

Releases

No release

Contributors

All

Activities

load more
can not load any more
1
https://gitee.com/caomeiyouren/simple-web-server.git
git@gitee.com:caomeiyouren/simple-web-server.git
caomeiyouren
simple-web-server
简单网站服务器
master

Search

102255 3a0e046c 1850385 102255 7aaa926c 1850385