Node.js http.createServer 简单服务配置
Posted yamboo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Node.js http.createServer 简单服务配置相关的知识,希望对你有一定的参考价值。
基本实现:
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 简单服务配置的主要内容,如果未能解决你的问题,请参考以下文章