Skip to content

Instantly share code, notes, and snippets.

@Nevin243
Created March 20, 2020 14:49
Show Gist options
  • Save Nevin243/dce3efc4fa702d8122a80615148dea36 to your computer and use it in GitHub Desktop.
Save Nevin243/dce3efc4fa702d8122a80615148dea36 to your computer and use it in GitHub Desktop.
Node & Flask API Example
# 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
// 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