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"
}
}