欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Node.js http.createServer 简单服务配置

程序员文章站 2023-11-22 20:55:34
基本实现: 解析GET参数: 解析POST: ......

基本实现:

var http = require("http");
var server = http.createserver(function (req, res) {
    if (req.url == "/") {
        res.end("hello world!");
    }
});

server.listen(8000);

解析get参数:

var http = require("http");
var qs = require("querystring");

var server = http.createserver(function (req, res) {

    var urlobj = url.parse(req.url);
    var query = urlobj.query;
    var queryobj = querystring.parse(query);
    if (req.url == "/") {
        res.end("hello world!" + " " +queryobj.user);
    }
});

server.listen(8000);

解析post:

var http = require("http");

var server = http.createserver(function (req, res) {

    if (req.url == "/") {
        req.on('data', function (chunk) {
            body += chunk;
        });

        req.on('end', function () {
            body = json.parse(body);
            res.end("hello world!" + " " +body.user);
        });
    }
});

server.listen(8000);