# -*- coding: utf-8 -*-
from flask import Flask
from flask import Flask
from flask import jsonify
from flask import request
from flask_pymongo import PyMongo
from flask_cors import CORS
app = Flask(__name__)
app.config['MONGO_DBNAME'] = 'zhihu'
app.config['MONGO_URI'] = 'mongodb://67.216.204.220:27017/zhihu'
mongo = PyMongo(app)
CORS(app, supports_credentials=True) # 用于处理跨域问题
@app.route('/api/all', methods=['GET'])
def get_all_stars():
table = mongo.db.zhihu_questions_answers
docs = []
for doc in table.find({}).limit(5):
doc.pop('_id')
docs.append(doc)
data = docs
print(data)
return jsonify({'data' : data,'status':'success'})
@app.route('/api/one', methods=['GET'])
def get_one():
table = mongo.db.zhihu_questions_answers
docs = []
for doc in table.find({}).limit(1):
doc.pop('_id')
docs.append(doc)
data = docs
print(data)
return jsonify({'data' : data,'status':'success'})
def after_request(response):
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'PUT,GET,POST,DELETE'
response.headers['Access-Control-Allow-Headers'] = 'Content-Type,Authorization'
return response
if __name__ == '__main__':
app.after_request(after_request)
app.run(port=5000, debug=True)