简体   繁体   中英

How to use the output of a node.js file inside jquery?

I looked around and found similar answers but none that fit my format or pattern.

I have the following TwitterNPM.js file that is node, that returns me a bunch of data:

var Twit = require('twit')

var T = new Twit({
    consumer_key:         'bla'
  , consumer_secret:      'bla'
  , access_token:         '35929248-bla'
  , access_token_secret:  'bla'
})



//get based on search term, count, location, etc
T.get('search/tweets', { q: 'beverly hills playhouse', count: 100}, function(err, data, response) {
  console.log(data)
})

and I had a jquery file set up like so:

$(function () {

    $("#search-button").click(function() {
        $.getJSON(
            //URL of web service
            "TwitterNPM.js"

        ).done(function (result) {
            console.log(result);

        });
    });
});

I guess the question is, can I even do something like this? All I want is to be able to use the output data of my node.js and put it into my HTML using jquery

You need a web server, see node express

var Twit = require('twit')

var T = new Twit({
    consumer_key:         'bla'
  , consumer_secret:      'bla'
  , access_token:         '35929248-bla'
  , access_token_secret:  'bla'
})

var express = require('express')
var app = express()

app.get('/tw', function (req, res) {
T.get('search/tweets', { q: 'beverly hills playhouse', count: 100}, function(err, data, response) {
    res.json(data)
  })
})

app.listen(80)

then on browser side

$(function () {

    $("#search-button").click(function() {
        $.getJSON(
            //URL of web service
            "http://localhost/tw"

        ).done(function (result) {
            console.log(result);

        });
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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