1. Cek Terlebih Dahulu Mongo Server di Terminal Menggunakan Perintah
sudo systemctl status mongod
2. Buka Mongo Compas untuk mendapatkan link koneksi ke Server Mongo
3. Edit File "app.py" Menjadi seperti di bawah inimongodb://localhost:27017
from flask import Flask, request, jsonifyfrom flask_pymongo import PyMongofrom werkzeug.security import generate_password_hashapp = Flask(__name__)secret_key = '123456'app.config['MONGO_URI'] = 'mongodb://localhost:27017/kampus'mongo = PyMongo(app)@app.route('/')def index():return 'Selamat Datang di API Mongo dan Flask'@app.route('/daftar',methods=['POST'])def daftar():data = request.form.to_dict()if mongo.db['mahasiswa'].find_one({'nim':data['nim']}):return jsonify({'info':'NIM Sudah Ada'})elif data['nim'] and data['nama'] and data['sandi']:id = mongo.db['mahasiswa'].insert_one({'nim': data['nim'],'nama':data['nama'],'sandi': generate_password_hash(data['sandi'])})return jsonify({'info': 'Berhasil Disimpan'})else:return jsonify({'info': 'Data Kosong/Lengkapi Data'})if __name__ == '__main__':app.run(host='0.0.0.0', port=5000, debug=True)
4. Jalan Project
pyton3 app.py
5. Uji Coba di Posman
Method : POSTURL : http://localhost:5000/daftarForm data :nim => 1001nama => Siswa 1sandi => 123
6. Lihat Hasil di Compas
0 komentar
Posting Komentar