如何从AngularJS应用的Python金字塔提供index.html文件? [英] How should an index.html file be served from Python Pyramid for an AngularJS app?

查看:61
本文介绍了如何从AngularJS应用的Python金字塔提供index.html文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AngularJS进入单页应用程序,但是我不使用Node或类似的东西,而是对服务器上的Python感到最满意.因此,鉴于我对Pyramid有点熟悉,我计划使用 pyramid_rpc 模块将JSON对象返回到客户端应用程序.这很简单,但是,为包含AngularJS初始AngularJS应用程序的起始索引文件提供服务的最佳方法是什么呢?通常,静态文件是从 static 目录提供的,但是从根目录提供 index.html 文件是否有问题?还是应该将可视图调用和'/'路由与渲染器一起使用到html模板?话虽这么说,金字塔对于这种应用来说是否会过分杀伤力?任何建议都很好.

I am getting into single-page apps with AngularJS but rather than using Node or similar, I am most comfortable with Python on the server. So, given I am somewhat familiar with Pyramid, I plan to use the pyramid_rpc module to return JSON objects to the client app. That's all straight forward enough, however, what then is the best way to serve the starting index file which contains the AngularJS initial AngularJS app? Usually, static files are served from a static directory, but is there any problem serving a index.html file from the root? Or should I use a view-callable and '/' route with a renderer to a html template? All that being said, is Pyramid overkill for this kind of application? Any advice would be great.

推荐答案

如果您打算返回一些JSON响应,那么金字塔是一个不错的选择.但是我不建议使用pyramid_rpc.JSON-RPC是旨在用于服务器之间的RPC通信的协议.直接的json响应更适合大多数客户端(例如浏览器),例如仅一堆返回JSON响应以响应GET/POST请求的路由.这也是一个提供 index.html 的好地方,可能带有一个很好的 http_cache 参数,以防止客户端过于频繁地请求该页面(当然,您可以走得更远优化此路线,但您可能应该将其保存以备后用.)

If you're planning to return some JSON responses then Pyramid is a great option. But I wouldn't recommend using pyramid_rpc. JSON-RPC is a protocol that is intended for RPC communication between servers. Straight json responses fit most clients (like browsers) better such as just a bunch of routes that return JSON responses in response to GET/POST requests. This is also a good place to serve up index.html, probably with a good http_cache parameter to prevent clients from requesting that page too often (of course you can go further with optimizing this route, but you should probably save that for later).

config.add_route('index', '/')
config.add_route('api.users', '/api/users')
config.add_route('api.user_by_id', '/api/users/{userid}')

@view_config(route_name='index', renderer='myapp:templates/index.html', http_cache=3600*24*365)
def index_view(request):
    return {}

@view_config(route_name='api.users', request_method='POST', renderer='json')
def create_user_view(request):
    # create a user via the request.POST parameters
    return {
        'userid': user.id,
    }

@view_config(route_name='api', request_method='GET', renderer='json')
def user_info_view(request):
    userid = request.matchdict['userid']
    # lookup user
    return {
        'name': user.name,
    }

这篇关于如何从AngularJS应用的Python金字塔提供index.html文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆