NodeJs implements a simple static web project

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326001160&siteId=291194637