Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const rqst = require("request")
- var express = require('express');
- var app = express();
- var http = require('http');
- const mysql = require('mysql');
- app.post('/getStuff', function(request, response) {
- var teamId = '';
- var matchId = '';
- var query;
- var body;
- const con = mysql.createConnection({
- host: 'myhost.net',
- user: 'myuser',
- password: 'mypw',
- database: 'mydatabase',
- });
- request.on('data', function(data) {
- global.data = JSON.parse(data);
- query = "my correct query";
- con.connect((err) => {
- if (err) {
- console.log('Error connecting to Db');
- return;
- }
- console.log('Connection was established');
- });
- let myJSON = [{
- "auth": false
- }]
- con.query(query, (err, rows) => {
- if (err) throw err;
- if (global.data.teamId == rows[0].homeTeam || global.data.teamId == rows[0].awayTeam) {
- var url = "http://www.myurl.com/" + global.data.matchId + ".json"
- // i can see the json file in my browser if i use this url, so it is correct
- console.log("the url to the json: " + url);
- rqst({
- url: url,
- json: true
- }, function (error, response, body) {
- if (!error && response.statusCode === 200) {
- let myJSON = body;
- // if i do a console.log(myJSON) here, it prints the correct json
- }
- })
- }
- // below prints 'undefined':
- console.log("nThe JSON resultset stringified: " + JSON.stringify(myJSON));
- response.end(JSON.stringify(myJSON));
- con.end();
- });
- console.log("DB connection ended");
- // this above is put inside the buffer stuff
- });
- // this seems to be happening before the sql stuff above... :S
- request.on('end', function() {
- // console.log("This was the query: " + query);
- response.setHeader("Content-Type", "text/json");
- response.setHeader("Access-Control-Allow-Origin", "*");
- });
- }); app.listen(8083);
Add Comment
Please, Sign In to add comment