剧场模式
首页后端Nodejs轻松学 nodejs - 基础篇

轻松学 Node.js - 基础篇 #15 web 服务器 part 7 使用 GET 或 POST 请求发送数据

海外散仙厉飞雨 · 真仙发布于
7

https://nodejs.org/dist/latest-v9.x/docs/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost

https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/

server.js

var http = require('http');
var fs = require('fs');
var url = require('url');
var querystring = require('querystring');

function startServer(route, handle) {
    var onRequest = function(request, response) {
        var pathname = url.parse(request.url).pathname;
        console.log('Request received ' + pathname);
        var data = [];
        request.on("error", function(err) {
            console.error(err);
        }).on("data", function(chunk) {
            data.push(chunk);
        }).on('end', function() {
            if (request.method === "POST") {
                if (data.length > 1e6) {
                    request.connection.destroy();
                }
                data = Buffer.concat(data).toString();
                route(handle, pathname, response, querystring.parse(data));
            } else {
                var params = url.parse(request.url, true).query;
                route(handle, pathname, response, params);
            }
        });
    }

    var server = http.createServer(onRequest);

    server.listen(3000, '127.0.0.1');
    console.log('Server started on localhost port 3000');
}

module.exports.startServer = startServer;

handler.js

var fs = require('fs');

function home(response) {
    response.writeHead(200, { 'Content-Type': 'text/html' });
    fs.createReadStream(__dirname + '/index.html', 'utf8').pipe(response);
}

function review(response) {
    response.writeHead(200, { 'Content-Type': 'text/html' });
    fs.createReadStream(__dirname + '/review.html', 'utf8').pipe(response);
}

function api_records(response, params) {
    response.writeHead(200, { 'Content-Type': 'application/json' });
    response.end(JSON.stringify(params));
}

module.exports = {
    home: home,
    review: review,
    api_records: api_records
}

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>hfpp2012</title>
</head>

<body>
    <form action="/api/v1/records" method="post">
        name: <input type="text" name="name" /> age: <input type="text" name="age" />
        <input type="submit" value="Submit">
    </form>
</body>

</html>
10 条回复
学员
dechuangchang · 金仙后期weblijingang · 真仙不乱方寸 · 真仙lgz · 金仙后期lzcdev · 真仙Johnny · 真仙heyengineer · 真仙foreverhot · 真仙修 · 真仙zjacker · 真仙VessieWoo · 真仙kelly · 大罗浅 · 真仙薄情人 · 道祖00Jane · 金仙后期samuelshang · 大罗itlaowang520 · 太乙巅峰mike652638 · 真仙iosLongFeng · 太乙巅峰zhanglongjiZLJ · 真仙与UIuoi89898 · 道祖wjm0913 · 太乙玉仙huang-guanhua · 真仙liaokui · 真仙qiaochunmei · 真仙qianduan · 真仙496087321 · 真仙Geassu · 道祖lihoubai · 真仙zbx7858 · 道祖
最新动态
dechuangchang · 金仙后期学习到了9:53
weblijingang · 真仙学习到了9:57
不乱方寸 · 真仙学习到了9:53
lgz · 金仙后期学习到了9:51
lzcdev · 真仙学习到了9:13

© 汕尾市求知科技有限公司 | 粤ICP备19038915号 | 在线学员:32

Top