http

typescript
const rawOrigins = process.env.ALLOWED_ORIGINS ?? "";

export const allowedOrigins = new Set(
  rawOrigins
    .split(",")
    .map((o) => o.trim())

CORS configuration that’s explicit (no *)

security http express
by codesnips 3 tabs
rust
use axum::{routing::get, Router, Json};
use serde::Serialize;

#[derive(Serialize)]
struct Response {
    message: String,

axum for type-safe async HTTP servers

rust async axum
by Marcus Chen 1 tab
go
package middleware

import (
  "net/http"

  "go.uber.org/zap"

Panic recovery middleware for HTTP servers

go http middleware
by Leah Thompson 1 tab
python
from sqlalchemy import select

from .models import Order, db


def stream_orders(batch_size=1000):

Stream a Large CSV Export in Flask With a Generator Response

flask streaming csv
by codesnips 3 tabs
go
package middleware

import (
  "log"
  "net/http"
  "runtime/debug"

Panic recovery middleware that fails closed and logs context

go http middleware
by Leah Thompson 1 tab
javascript
const jwt = require('jsonwebtoken');

const ACCESS_SECRET = process.env.JWT_ACCESS_SECRET;
const REFRESH_SECRET = process.env.JWT_REFRESH_SECRET;
const ISSUER = 'api.example.com';
const AUDIENCE = 'example-web';

Sign and Verify JWT Access Tokens in Express Auth Middleware

express jwt authentication
by codesnips 3 tabs
typescript
export type ErrorCode =
  | 'VALIDATION'
  | 'UNAUTHENTICATED'
  | 'FORBIDDEN'
  | 'NOT_FOUND'
  | 'CONFLICT'

API error shape that frontend can rely on

typescript express react
by codesnips 4 tabs
ruby
class Problem
  DEFAULT_TYPE = "https://api.example.com/problems/about:blank".freeze

  attr_reader :type, :title, :status, :detail, :instance, :extensions

  def initialize(status:, title:, detail: nil, type: nil, instance: nil, extensions: {})

API Error Handling with Problem Details (RFC7807-ish)

rails api error-handling
by codesnips 3 tabs
go
package web

import (
  "bytes"
  "html/template"
  "net/http"

Template rendering with html/template and strict escaping

go templates http
by Leah Thompson 1 tab
javascript
const jwt = require('jsonwebtoken');

const SECRET = process.env.JWT_SECRET;
const ALGORITHM = 'HS256';
const ACCESS_TTL = '15m';

JWT Authentication Middleware in Express That Populates req.user

express jwt authentication
by codesnips 3 tabs
go
package feed

import "time"

type Event struct {
	ID        string    `json:"id"`

Stream and Decode Newline-Delimited JSON (NDJSON) from an HTTP Response Body in Go

go ndjson streaming
by codesnips 3 tabs
ruby
module Retryable
  module_function

  def with_retries(tries: 3, base: 0.3, cap: 5.0, on: [StandardError])
    attempt = 0
    begin

Retry a Flaky HTTP Call with Exponential Backoff Using a Rails Service Object

rails http retries
by codesnips 3 tabs