简体   繁体   English

使用节点JS从浏览器的用户输入中检索Twitter

[英]Using node JS to retrieve twitter from user input from browser

I'm new to using nodeJS and npm modules. 我是使用nodeJS和npm模块的新手。

Using the module twit , I can do a GET request to the Twitter API and search tweets. 使用模块twit ,我可以对Twitter API进行GET请求并搜索tweet。

var Twit = require('twit')

var T = new Twit({ `login details`})

T.get('search/tweets', { q: 'banana since:2011-07-11', count: 100 }, function(err, data, response) {
  console.log(data)
})

I am trying to build a simple user browser page, where a user can enter their own query parameters. 我正在尝试构建一个简单的用户浏览器页面,用户可以在其中输入自己的查询参数。 I understand that modules are server side, and we can't use them on the browser. 我知道模块是服务器端的,我们不能在浏览器中使用它们。 I also can't use browserify because twitter uses Oauth method which means you can't access it from the browser. 我也不能使用browserify因为twitter使用Oauth方法,这意味着您无法从浏览器访问它。

Is there a way to pass query parameters from the browser back to the server code, and then to pass that result back to the browser? 有没有一种方法可以将查询参数从浏览器传递回服务器代码,然后将结果传递回浏览器?

T.get('search/tweets', { q: {user options}, function(err, data, response) {
  console.log(data)
})

We can use socket.io for the streaming twitter data, but how can I use it for the REST API? 我们可以将socket.io用于流式Twitter数据,但是如何将其用于REST API?

Nodejs 的NodeJS

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const Twit = require('twit');

...
const T = new Twit(LOGIN_DETAILS);

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/search', function (req, res, next) {
  T.get('search/tweets', {q: req.body.searchQuery, count: 100}, function (err, data, response) {
    return res.json({
      data: data
    });
  });
});

Client Side 客户端

...    
$.ajax({
  url: "{API_URL}/search",
  type: "POST",
  dataType: 'json',
  data: JSON.stringify({
    searchQuery: SearchQuery
  }),
  contentType: "application/json; charset=utf-8",
  success: function (msg) {
    console.log(msg);
    // do something with the response
  }
});   

$ is jQuery, read more about jquery ajax $是jQuery,了解有关jquery ajax的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM