基本實現: 解析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);