我正在使用nodejs后端(在this tutorial之后将推文拉入我的应用程序的前端。
现在我已经准备好部署到开发服务器了,我已经使用ng build --prod
打包了前端,除了带有加载推文的模块外,它看起来很好用。如何托管应用程序的节点服务器部分以正确显示推文?
以下是我的节点应用的文件。它存放在我的项目文件夹的根目录中,在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"
}
}
答案 0 :(得分:1)
我建议你在Heroku上托管你的nodeJS应用程序。 您可以开始使用Here。
对于Angular App,我建议您使用Firebase。托管角度应用程序的最简单方法。 How to deploy Angular apps on Firebase