码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2019-04-05 12:30:01      阅读:1238      评论:0      收藏:0      [点我收藏+]

标签:实现   func   url   create   node   creat   cti   str   get   

基本实现:

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);

 

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

标签:实现   func   url   create   node   creat   cti   str   get   

原文地址:https://www.cnblogs.com/yamboo/p/10658309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!