2016-12-16 64 views
0

我在Bottle和Mongo數據庫的幫助下嘗試使用一般的REST API。mongo db的瓶子應用程序

,我得到網頁上的地址127.0.0.1:8010/的錯誤是

Error: 404 Not Found

Sorry, the requested URL http ://127.0.0.1:8010/ caused an error:

Not found: '/'

在命令行中,我得到這個:

$ python myrestapi.py 

Bottle v0.12.10 server starting up (using WSGIRefServer())... 
Listening on "http://127.0.0.1:8010/" 
Hit Ctrl-C to quit. 
127.0.0.1 - - [17/Dec/2016 01:54:46] GET/HTTP/1.1 404 720 

這裏是我的代碼對於文件/ myrestapi.py:

import json 
import bottle 

from bottle import route, run, request, abort 
from pymongo import Connection 

connection = Connection('localhost', 27017) 
db = connection.mydatabase 
app = bottle.Bottle() 

@app.route('/documents', method='PUT') 
def put_document(): 
    data = request.body.readline() 

    if not data: 
    abort(400, 'No data received') 
    entity = json.loads(data) 

    if not entity.has_key('_id'): 
    abort(400, 'No _id specified') 

try: 
    db['documents'].save(entity) 

    except ValidationError as ve: 

    abort(400, str(ve)) 

@app.route('/documents/:id', method='GET') 
def get_document(id): 
entity = db['documents'].find_one({'_id':id}) 
if not entity: 
    abort(404, 'No document with id %s' % id) 
return entity 

bottle.run(host='localhost', port=8010) 
+0

我修正了垂直間距,但請修正Python代碼的縮進 –

+0

另外,你有n o在'/'設置的路由,所以你嘗試過'/ documents/1',或者任何ID? –

回答