Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2014-04-17代码游戏 #32

Open
jcm872000 opened this issue Apr 17, 2014 · 2 comments
Open

2014-04-17代码游戏 #32

jcm872000 opened this issue Apr 17, 2014 · 2 comments

Comments

@jcm872000
Copy link

node.js月过去一半了,大家应该对此也有了一定的理解。之前我们已经使用node.js进行了文件处理、网页抓取,便于我们的工作生活。接下来我们要了解下node.js碉堡的特点了。
what?可以用js来整服务器?前端程序猿的福音来了,妈妈终于不用担心我被后端欺负了。不错。今天我们就是要用node.js做个网站,丢开你的Apache、Nginx、tomcat吧。
题目要求:
1、网站!看清楚了,不是网页,不是写个js跑跑就行了,而是要可以被其他PC机的浏览器使用的哦。
2、网站提供数字是否是奇数/偶数的判断功能,页面随意,可看下示例:
clip 04-17-20-11-33
大致上需要提供get(页面显示)、post(提交输入的内容)、数字判断功能。提交也可以用form表单,判断结果输出用别的方式也行,不过不能使用服务端的console输出,得给用户看到。
3、如果时间够的话,请再增加判断数字是质数/合数的功能。
4、无法在现场携带电脑展示的,请提供网站地址。

@chenkan
Copy link
Owner

chenkan commented Apr 17, 2014

var express = require('express');
var app = express();

var page = '<form method="post" action="/"><input type="text" name="username"> <input type="submit"></form>'

// app.use(express.json());       // to support JSON-encoded bodies
// app.use(express.urlencoded()); // to support URL-encoded bodies
app.use(express.bodyParser());

app.get('/', function(req, res){
  res.send(page);
});

app.post('/', function(req, res) {
    var num = req.body.username;
    var isOdd = num % 2 == 0 ? "Even" : "Odd";
    res.send('The Num is: ' + isOdd);
});


var server = app.listen(3000, function() {
    console.log('Listening on port %d', server.address().port);
});

@bingohuang
Copy link
Collaborator

server.js

var http = require("http"),
    url  = require("url"),
    path = require("path"),
    fs   = require("fs");

http.createServer(function (req, res) {
    var pathname=__dirname+url.parse(req.url).pathname;
    if (path.extname(pathname)=="") {
        pathname+="/";
    }
    if (pathname.charAt(pathname.length-1)=="/"){
        pathname+="index.html";
    }

    fs.exists(pathname,function(exists){
        if(exists){
            switch(path.extname(pathname)){
                case ".html":
                    res.writeHead(200, {"Content-Type": "text/html"});
                    break;
                case ".js":
                    res.writeHead(200, {"Content-Type": "text/javascript"});
                    break;
                case ".css":
                    res.writeHead(200, {"Content-Type": "text/css"});
                    break;
                case ".gif":
                    res.writeHead(200, {"Content-Type": "image/gif"});
                    break;
                case ".jpg":
                    res.writeHead(200, {"Content-Type": "image/jpeg"});
                    break;
                case ".png":
                    res.writeHead(200, {"Content-Type": "image/png"});
                    break;
                default:
                    res.writeHead(200, {"Content-Type": "application/octet-stream"});
            }

            fs.readFile(pathname,function (err,data){
                res.end(data);
            });
        } else {
            res.writeHead(404, {"Content-Type": "text/html"});
            res.end("<h1>404 Not Found</h1>");
        }
    });

}).listen(8080, "127.0.0.1");

console.log("Server running at http://127.0.0.1:8080/");

index.js

<!DOCTYPE html>
<html>
<head>
    <title>bingo test</title>
</head>
<body>
hello bingo<br/>
<input type='text' id='txt'><br/>
<button type="button" id='btn'>Test!</button><br/>
<form action="" method="get">
  input number: <input type="text" name="fname" />
  <input type="submit" value="Submit" />
</form>
</body>
</html>
<script type="text/javascript" src="bingo.js"></script>

bingo.js

// 发布按钮
document.getElementById("btn").onclick = function () {
    //alert("bingo");
    var num = document.getElementById("txt").value;
    if(num%2 == 0) {
        alert("偶数");
    } else if(num%2==1) {
        alert("奇数");
    } else {
         alert("不是整数");
    }

};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants