Created
March 20, 2020 14:49
-
-
Save Nevin243/dce3efc4fa702d8122a80615148dea36 to your computer and use it in GitHub Desktop.
Node & Flask API Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For running a simple flask app | |
# Run via; export FLASK_APP = simple_flask_example.py | |
# python -m flask run | |
from flask import Flask, json | |
app = Flask(__name__) | |
def do_something(): | |
# Go do something, normally this is seperated out in a seperate file / class | |
return "Hello world" | |
@app.route('/test') | |
def test(): | |
data = do_something() | |
response = app.response_class( | |
response=json.dumps(data), | |
status=200, | |
mimetype='application/json' | |
) | |
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To run; | |
// npm init | |
// node test.js | |
const https = require('http'); | |
https.get('http://localhost:5000/test', (resp) => { | |
let data = ''; | |
// A chunk of data has been recieved. | |
resp.on('data', (chunk) => { | |
data += chunk; | |
}); | |
// The whole response has been received. Print out the result. | |
resp.on('end', () => { | |
console.log(JSON.parse(data)); | |
}); | |
}).on("error", (err) => { | |
console.log("Error: " + err.message); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment