NodeJs实现简单的静态web项目

  • main.js
//noinspection JSUnresolvedFunction
/**
 * Created by hyman on 2017/9/17.
 */
var http = require("http");
//noinspection JSUnresolvedFunction
var fs = require("fs");
//noinspection JSUnresolvedFunction
var app = http.createServer(function (req,resp) {
    if(req.method === "GET"){
        switch (req.url){
            case "/":
            case "/index.html":
                //noinspection JSUnresolvedFunction
                fs.readFile("./html/index.html",function (err,data) {
                    if(err) throw err;//抛出异常
                    //noinspection JSUnresolvedFunction
                    resp.writeHead(200,{"Content_Type":"text/html"});//设置响应格式
                    resp.write(data.toString());
                    resp.end();
                });
                break;
            case "/add.html":
                //noinspection JSUnresolvedFunction
                fs.readFile("./html/add.html",function (err,data) {
                    if(err) throw err;//抛出异常
                    //noinspection JSUnresolvedFunction
                    resp.writeHead(200,{"Content_Type":"text/html"});//设置响应格式
                    resp.write(data.toString());
                    resp.end();
                });
                break;
            case "/remove.html":
                //noinspection JSUnresolvedFunction
                fs.readFile("./html/remove.html",function (err,data) {
                    if(err) throw err;//抛出异常
                    //noinspection JSUnresolvedFunction
                    resp.writeHead(200,{"Content_Type":"text/html"});//设置响应格式
                    resp.write(data.toString());
                    resp.end();
                });
                break;
            case "/find.html":
                //noinspection JSUnresolvedFunction
                fs.readFile("./html/find.html",function (err,data) {
                    if(err) throw err;//抛出异常
                    //noinspection JSUnresolvedFunction
                    resp.writeHead(200,{"Content_Type":"text/html"});//设置响应格式
                    resp.write(data.toString());
                    resp.end();
                });
                break;
            case "/edit.html":
                //noinspection JSUnresolvedFunction
                fs.readFile("./html/edit.html",function (err,data) {
                    if(err) throw err;//抛出异常
                    //noinspection JSUnresolvedFunction
                    resp.writeHead(200,{"Content_Type":"text/html"});//设置响应格式
                    resp.write(data.toString());
                    resp.end();
                });
                break;
            default:
                var html = "<html><head><meta charset='UTF-8'>" +
                    "<title>测试网</title></head><body>" +
                    "404 Not find" +
                    "</body></html>";

                //noinspection JSUnresolvedFunction
                resp.writeHead(404, {"Conten-Type": "text/html"});
                resp.end(html);
        }
    }else{
        console.log("Not supported");//提示:不支持
    }
});

//noinspection JSUnresolvedFunction
app.listen(7798);
  • add.htnl
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>add</title>
</head>
<body>
<h1>add</h1>
<form action="add.js" method="post">
    <div>
        <span>Domain Name:</span>
        <input type="text" name="domainName">
    </div>
    <div>
        <span>Name:</span>
        <input type="text" name="name">
    </div>
    <div>
        <span>Email:</span>
        <input type="text" name="email">
    </div>
    <div>
        <span>Age:</span>
        <input type="text" name="age">
    </div>
    <div>
        <span>Click:</span>
        <input type="text" name="click">
    </div>
    <input type="submit" value="Add">
</form>


</body>
</html>
  • remove.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>remove</h1>
<form action="remove.js" method="post">
    <div>
        <span>Domain Name:</span>
        <input type="text">
    </div>
    <input type="submit" value="Remove">
</form>
</body>
</html>
  • edit.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>edit</h1>
<form action="edit.js" method="post">
    <div>
        <span>Domain Name:</span>
        <input type="text">
    </div>
    <div>
        <span>Name:</span>
        <input type="text">
    </div>
    <div>
        <span>Email:</span>
        <input type="text">
    </div>
    <div>
        <span>Age:</span>
        <input type="text">
    </div>
    <div>
        <span>Click:</span>
        <input type="text">
    </div>
    <input type="submit" value="Update">
</form>
</body>
</html>
  • find.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>find</h1>
<form action="find.js" method="post">
    <div>
        <span>Domain Name:</span>
        <input type="text">
    </div>
    <input type="submit" value="Find">
</form>
</body>
</html>
  • index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
</head>
<body>
<h1>欢迎来到测试网</h1>
<a href="/add.html">add</a>&nbsp;&nbsp;
<a href="/remove.html">remove</a>&nbsp;&nbsp;
<a href="/edit.html">edit</a>&nbsp;&nbsp;
<a href="/find.html">find</a>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值