skip to Main Content

I have create a Node application that uses the Twit(twitter api) to allow my ionic/Angular Application to post a tweet on twitter, however this problem that I have is that i get a 404 error message when I set the REST method to Post, it seems to work with a GET method.

However I do not know how I can dynamically change the Data in my node application from my Ionic Application.

I want to change the User’s information and the Message that is being sent, but I do not know where to start. if anyone can guide me that will be appriecated.

this is my Node server.js file

const express = require('express');
const Twitter = require('twit');

const app = express();
const client = new Twitter({
  consumer_key:         '...',
  consumer_secret:      '...',
  access_token:         '...',
  access_token_secret:  '...',
});


app.use(require('cors')());
app.use(require('body-parser').json());

app.post('/post_tweet', (req, res) => {

  tweet = {status:"Random"};

    client
      .post(`statuses/update`, tweet)
      .then(timeline => {
        console.log(timeline);

        res.send(timeline);
      })

     .catch(error => {
      res.send(error);
    });


});

app.listen(3000, () => console.log('Server running'));

this is my twitter service in my Ionic application

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';
@Injectable({
  providedIn: 'root'
})
export class TwitterserviceService {
  api_url = 'http://localhost:3000';

  constructor(private http: HttpClient) { } 

  tweet(tweetdata: string) {
    return this.http.get<any>(`${this.api_url}/post_tweet`)
        .pipe(map(tweet => {

            alert("tweet posted")

            return tweet;
        }));
}
}

and this is the method that I use to send a Post, however the message “this works” doesent post instead the default message in the Node application is sent “random”



sendTweet() {    
  this.api.tweet('this works')
            .pipe(first())
            .subscribe(
                data => {
                    console.log('yes')
                },
                error => {
                  'failed'
                });
}

2

Answers


  1. Chosen as BEST ANSWER

    Alright I have found the answer and it was actually quite simple here it the link to the resource that i am using => https://code.tutsplus.com/tutorials/connect-to-the-twitter-api-in-an-angular-6-app--cms-32315

    this is my node js code

    const express = require('express');
    const Twitter = require('twit');
    
    const app = express();
    const client = new Twitter({
      consumer_key:         '...',
      consumer_secret:      '...',
      access_token:         '...',
      access_token_secret:  '...',
    });
    
    
    app.use(require('cors')());
    app.use(require('body-parser').json());
    
    app.post('/post_tweet', (req, res) => {
    
      tweet = req.body;
    
      client
        .post(`statuses/update`, tweet)
        .then(tweeting => {
          console.log(tweeting);
    
          res.send(tweeting);
        })
    
       .catch(error => {
        res.send(error);
      });
    
    
    });
    
    app.listen(3000, () => console.log('Server running'));
    

    and here it the code that I have in my Ionic/Angular Project

    api_url = 'http://localhost:3000';
    
      tweet(tweetdata: string) {
        return this.http.post<any>(`${this.api_url}/post_tweet/`, {status: tweetdata})
            .pipe(map(tweet => {
    
                alert("tweet posted")
    
                return tweet;
            }));
    }
    sendTweet() {  
      this.tweet('This is app code')
                .pipe(first())
                .subscribe(
                    data => {
                        console.log('yes')
                    },
                    error => {
                      'failed'
                    });
    }
    

    hope this helps someone.


  2. Your service should do a POST, not a GET. And a POST must have a body.

    tweet(tweetdata: string) {
        return this.http.post<any>(`${this.api_url}/post_tweet`, { tweetdata })
    }
    

    note that you will have to handle this body in the express route and probably do something with this tweetdata attribute.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search