Node.js http.createServer 简单服务配置
程序员文章站
2022-06-29 11:12:32
基本实现: 解析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);
上一篇: 朋友圈微商被黑:利用作弊软件制造骗局
下一篇: vsCode中误删了文件,教你怎么恢复