|
|
from flask import Flask, jsonify |
|
|
from flask_cors import CORS |
|
|
|
|
|
app = Flask(__name__) |
|
|
CORS(app) |
|
|
|
|
|
|
|
|
users = [ |
|
|
{"id": 1, "name": "John Doe", "email": "john@example.com"}, |
|
|
{"id": 2, "name": "Jane Smith", "email": "jane@example.com"}, |
|
|
{"id": 3, "name": "Bob Johnson", "email": "bob@example.com"} |
|
|
] |
|
|
|
|
|
|
|
|
@app.route('/api/users', methods=['GET']) |
|
|
def get_users(): |
|
|
return jsonify(users) |
|
|
|
|
|
|
|
|
@app.route('/api/users/<int:user_id>', methods=['GET']) |
|
|
def get_user(user_id): |
|
|
user = [user for user in users if user['id'] == user_id] |
|
|
if user: |
|
|
return jsonify(user[0]) |
|
|
else: |
|
|
return jsonify({"error": "User not found"}), 404 |
|
|
|
|
|
if __name__ == '__main__': |
|
|
app.run(debug=True) |