I have been working on a project which is to create a node.js server and I have to read some text from the file. Then I want to use a Python API to consume these text and get the result back. I have been able to hit these API and get the response successfully.
What have I done till now -
1. Created a node server
2. read all the file and get the text content from them
3. create a flask server with appropriate api
4. hit these API to get the response
app.js
var bodyParser = require('body-parser');
var request = require('request-promise');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/', function(req, res){
res.send('Node server')
})
app.get('/postdatatoFlask', function (req, res) {
var fs = require('fs');
categories = ['business', 'entertainment']
// answers SHOULD CONTAIN ALL THE RESPONSES FROM THE FLASK API
var answers = []
//iterating over different file
categories.forEach(function(category){
for(var i=100;i<=100;i++){
//creating the path for the new file to be read
var path = 'dataset/'+category+'/'+i.toString()+'.txt';
//textContent contains the data read from file
var textContent = fs.readFileSync(path, 'utf8')
//creating the object
var data = { content : textContent }
//posting the data to the flask server on route postdata
var options = {
method: 'POST',
uri: 'http://127.0.0.1:5000/postdata',
body: data,
json: true
};
// returndata is the variable contains the data return by flask API
var returndata
var sendrequest = request(options)
.then(function (parsedBody) {
// parsedBody contains the data sent back from the Flask server
returndata = parsedBody;
answers.push(returndata)
console.log(returndata) // do something with this data, here I'm assigning it to a variable.
})
.catch(function (err) {
console.log(err);
});
}
})
console.log('Showing the responses!!!')
console.log(answers)
console.log('Stoping the showcase!!!!')
res.send('done');
});
app.listen(3000);
compute.py
import json
import time
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from io import StringIO
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
import pandas as pd
app = Flask(__name__)
@app.route('/')
def index():
return "Flask server"
@app.route('/postdata', methods = ['POST'])
def postdata():
data = request.get_json()
# print(data['content'])
textContent = data['content']
print(textContent)
print('-- --- -- --- -- -- -- -- -- --')
return json.dumps({"newdata":textContent})
if __name__ == "__main__":
app.run(port=5000)
Problem
1. In app.js I want to store all the responses in answer array but I am unable to populate it.
2. why output from console.log(returndata) print after console.log(answer)
for(var i=100;i<=100;i++), is there a typing mistake in this part?i=100and then loop terminates)? Why not remove the loop if it serves no purpose?