(X)读<了不起的Node.js>-12.一个Twitterwe

2018-08-07  本文已影响0人  在路上的海贼

创建模块

发送一个简单的HTTP其你去

const http = require('http');

http.createServer(function (req, res) {
    res.writeHeader(200);
    res.end('hello world');

}).listen(3000);


const http = require('http');

http.request({
    host: '127.0.0.1',
    port: '3000',
    url: '/',
    method: 'GET'
}, function (res) {
    let body = '';
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        body += chunk;
    });
    res.on('end', function () {
        console.log('\n We got:\033[96m' + body + '\033[39m\n');
    });
}).end();

发送数据

let http = require('http'), qs = require('querystring');

function send(theName) {
    http.request({
        host: '127.0.0.1',
        port: 3000,
        url: '/',
        method: 'POST'
    }, function (res) {
        res.setEncoding('utf8');
        res.on('end', function () {
            console.log('\n  \033[96m request complete! \033[39m');
            process.stdout.write('\n your name:   ');
        });
    }).end(qs.stringify({name: theName}));
}

process.stdout.write('\n your name:  ');
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (name) {
    send(name.replace('\n', ''));
});




获取推文

这个已经过期了...更新版本了

The Twitter REST API v1 is no longer active. Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.

上一篇下一篇

猜你喜欢

热点阅读