Skip to content

Instantly share code, notes, and snippets.

@Chion82
Created July 4, 2015 12:34
Show Gist options
  • Save Chion82/29fbadbd0d7ff888a09d to your computer and use it in GitHub Desktop.
Save Chion82/29fbadbd0d7ff888a09d to your computer and use it in GitHub Desktop.
Backend server for nodejs_midway sample
from flask import Flask, jsonify, request, make_response
from urllib import unquote
app = Flask(__name__)
@app.route('/api/user', methods=['GET'])
def api_user():
if (request.cookies.get('test_cookie') != None):
test_cookie = unquote(request.cookies['test_cookie'])
else:
test_cookie = 'NOT SET'
resp = make_response(jsonify({
"status":200,
"user_info" : {
"username" : "testuser",
"nickname" : "Test User",
"email" : "test@test.com",
"phone" : "18000000000",
"avatar" : "/images/avatars/default.jpg"
},
"cookies" : {
"test_cookie" : test_cookie
}
}))
resp.set_cookie('test_cookie', 'Set by back-end server.')
return resp
@app.route('/api/post', methods=['POST'])
def api_post():
if (request.form.get('data') != None):
post_data = request.form['data']
else:
post_data = 'NO POST DATA'
return jsonify({
"status" : 200,
"post_data" : post_data
})
@app.route('/api/rest', methods=['GET'])
def api_rest():
a = request.args.get('a') if request.args.get('a') else 0
b = request.args.get('b') if request.args.get('b') else 0
return jsonify({
'status' : 200,
'sum' : int(a)+int(b)
})
if __name__ == '__main__':
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment