issue_comments: 1499491258
This data as json
html_url | issue_url | id | node_id | user | created_at | updated_at | author_association | body | reactions | issue | performed_via_github_app |
---|---|---|---|---|---|---|---|---|---|---|---|
https://github.com/simonw/datasette/issues/2054#issuecomment-1499491258 | https://api.github.com/repos/simonw/datasette/issues/2054 | 1499491258 | IC_kwDOBm6k_c5ZYGu6 | 9599 | 2023-04-06T19:00:16Z | 2023-04-06T20:40:16Z | OWNER | I asked GPT-4 to compare URL routing based on path and verb approaches in different languages and frameworks and spit out some examples. Full transcript here: https://gist.github.com/simonw/fa2379b97420404a81b0fcdb4db79657 Initial prompt: > Show me code examples of different web frameworks in Python and JavaScript and Go illustrating how HTTP routing works - in particular the problem of mapping an incoming HTTP request to some code based on both the URL path and the HTTP verb > > Don't show installation instructions, just get straight to the code snippets Flask: ```python from flask import Flask app = Flask(__name__) @app.route('/example', methods=['GET']) def get_example(): return "This is a GET request." @app.route('/example', methods=['POST']) def post_example(): return "This is a POST request." if __name__ == '__main__': app.run() ``` Express: ```javascript const express = require('express'); const app = express(); app.get('/example', (req, res) => { res.send('This is a GET request.'); }); app.post('/example', (req, res) => { res.send('This is a POST request.'); }); app.listen(3000, () => { console.log('Server is running on port 3000'); }); ``` Gin: ```go package main import ( "github.com/gin-gonic/gin" ) func main() { r := gin.Default() r.GET("/example", func(c *gin.Context) { c.String(200, "This is a GET request.") }) r.POST("/example", func(c *gin.Context) { c.String(200, "This is a POST request.") }) r.Run() } ``` Actix-web: ```rust use actix_web::{web, App, HttpResponse, HttpServer, Responder}; async fn get_example() -> impl Responder { HttpResponse::Ok().body("This is a GET request.") } async fn post_example() -> impl Responder { HttpResponse::Ok().body("This is a POST request.") } #[actix_web::main] async fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() .route("/example", web::get().to(get_example)) .route("/example", web::post().to(post_example)) }) .bind("127.0.0.1:8080")? .run() .await } ``` Koa: ```javascript const Koa = require('koa'); const Router = require('koa-router'); const app = new Koa(); const router = new Router(); router.get('/example', (ctx) => { ctx.body = 'This is a GET request.'; }); router.post('/example', (ctx) => { ctx.body = 'This is a POST request.'; }); app.use(router.routes()); app.use(router.allowedMethods()); app.listen(3000, () => { console.log('Server is running on port 3000'); }); ``` Django: ```python from django.http import HttpResponse from django.views import View class ExampleView(View): def get(self, request): return HttpResponse("This is a GET request.") def post(self, request): return HttpResponse("This is a POST request.") ``` ```python from django.urls import path from .views import ExampleView urlpatterns = [ path('example/', ExampleView.as_view(), name='example'), ] ``` Rocket: ```rust #![feature(proc_macro_hygiene, decl_macro)] #[macro_use] extern crate rocket; use rocket::response::content; #[get("/example")] fn get_example() -> content::Plain<&'static str> { content::Plain("This is a GET request.") } #[post("/example")] fn post_example() -> content::Plain<&'static str> { content::Plain("This is a POST request.") } #[launch] fn rocket() -> _ { rocket::build().mount("/", routes![get_example, post_example]) } ``` Elixir/Phoenix: ```elixir defmodule ExampleAppWeb.Router do use ExampleAppWeb, :router pipeline :api do plug :accepts, ["json"] end scope "/api", ExampleAppWeb do pipe_through :api get "/example", ExampleController, :get_example post "/example", ExampleController, :post_example end end ``` Bottle: ```python from bottle import Bottle, run app = Bottle() @app.route('/example', method='GET') def get_example(): return "This is a GET request." @app.route('/example', method='POST') def post_example(): return "This is a POST request." if __name__ == '__main__': run(app, host='localhost', port=8080) ``` FastAPI: ```python from fastapi import FastAPI app = FastAPI() @app.get("/example") async def get_example(): return {"message": "This is a GET request."} @app.post("/example") async def post_example(): return {"message": "This is a POST request."} if __name__ == "__main__": import uvicorn uvicorn.run(app, host="127.0.0.1", port=8000) ``` | {"total_count": 0, "+1": 0, "-1": 0, "laugh": 0, "hooray": 0, "confused": 0, "heart": 0, "rocket": 0, "eyes": 0} | 1657861026 |