1

I'm using a nodejs backend (following this tutorial to pull in tweets into the front end of my application.

Now that I'm ready to deploy to a development server, I've packaged the frontend with ng build --prod, and that looks and works fine, except for the module with loading tweets. How do I host the node server part of the application to display the tweets properly?

Here are the files of my node app. It's held in the root of my project folder, outside of src.

server.js

var express = require('express');
var bodyParser = require('body-parser');
var cors = require('cors');
var functions = require('./functions');

var app = express();

app.use(bodyParser.urlencoded({extended: true}));
app.use(cors());
app.post('/authorize', functions.authorize);
app.post('/search', functions.search);


app.listen(3000);
console.log('listening now');

functions.js

var request = require('request');
var config = require('./config');

functions = {
    authorize: function(req, res) {
        var header = config.consumerkey + ':' +config.consumersecret;
        var encheader = new Buffer(header).toString('base64');
        var finalheader = 'Basic ' + encheader;

        request.post('https://api.twitter.com/oauth2/token', {form: {'grant_type': 'client_credentials'},
        headers: {Authorization: finalheader}}, function(error, response, body) {
            if(error)
            console.log(error);
            else {
                config.bearertoken = JSON.parse(body).access_token;

                res.json({success: true, data:config.bearertoken});
            }

        })
    },

    search: function(req, res) {
      var searchquery = req.body.query;
      var encsearchquery = encodeURIComponent(searchquery);
      var bearerheader = 'Bearer ' + config.bearertoken;
      request.get('https://api.twitter.com/1.1/search/tweets.json?q=' + encsearchquery +
       '&result_type=recent', {headers: {Authorization: bearerheader}}, function(error, body, response) {
           if(error)
           console.log(error);
           else {
               res.json({success: true, data:JSON.parse(body.body)});
           }
       })
  }
}
module.exports = functions;

config.js

var appsettings = {
  consumerkey: 'key',
  consumersecret: 'key',
  bearertoken: ''
};

module.exports = appsettings;

package.json

{
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "start": "node server",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "brooklynbrim",
  "license": "MIT",
  "devDependencies": {
    "body-parser": "^1.17.2",
    "cors": "^2.8.4",
    "express": "^4.15.4",
    "request": "^2.81.0"
  }
}

1 Answer 1

1

I would recommend you to host your nodeJS app on Heroku. You can get started Here.

And for the Angular App, I would recommend you Firebase. Easiest way to host your angular app. How to deploy Angular apps on Firebase

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, I'll give it a shot! Would any of the port information need to change once it's on Heroku? @Juan Camilo Giraldo Chaverra
Perfect solution! the heroku tutorial works. I have a (work provided development) server that I have to use, but I'll still look into firebase for my own projects. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.