Home >Web Front-end >JS Tutorial >Node.js GET/POST requests

Node.js GET/POST requests

黄舟
黄舟Original
2017-01-17 16:07:181144browse

Node.js GET/POST request

Get GET request content

Case: get.js

[code]var http = require('http');
var url = require('url');
var util = require('util');
http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type' : 'text/plain'});
    res.end(util.inspect(url.parse(req.url, true)));
}).listen(8888);

Terminal:

Node.js GET/POST requests

Client: http://localhost:8888/user?name=w3c&email=w3c@w3cschool.cc

Node.js GET/POST requests

Get POST request content

Case: post.js

[code]var http = require('http');
var querystring = require('querystring');
var util = require('util');
http.createServer(function (req, res) {
    // post 发送的数据
    var post = 'username=zhang&age=23';
    req.on('data', function (chunk) {
        post += chunk;
    });
    req.on('end', function () {
        post = querystring.parse(post);
        console.log(post);
        res.end(util.inspect(post));
    })
}).listen(8888);

The above is the content of Node.js GET/POST request. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn