snz

0.1.1 • Public • Published

snz - the ridiculously simple way to create a web service

snz - it's pronounced "essence" - is a web framework focused on building simple HTTP servers with zero boilerplate.

Create a directory:

mkdir my_server

Launch snz:

npx snz my_server

and you're ready to go. snz will serve up whatever you put in that directory. No package.json. No create-snz-app or snz init and answering 17 questions. Just point npx snz at a directory.

snz serves up static files (boring...)

Add a static file (index.html, hello.txt, ...) and snz will serve those files up as is.

Add a JSON file (index.json, /mock/api/users/1.json, ...) and snz will serve it up with the appropriate headers.

snz lets you write API endpoints with zero boilerplate (interesting!)

Add a .js or .ts file to your server directory and snz will execute it and serve the result.

folder structure defines the routing

/api/users/food.ts handles requests to /api/users/food

snz will call the function you define in that file. The return value of that function is the response body. Zero boilerplate.

export default function(){
  return "hello, world"
}

return an object and snz will turn it into JSON

export default function(){
  return {"hello": "world"}
}

path parameters

Dynamic path parameters work as you'd expect: add /api/users/:userId/food.ts and your function in food.ts can access userId as a path param (with zero boilerplate)

export default function({pathParams}){
  const user = pathParams.userId
  return `greetings, user #${user}`
}

Everything else you need to put together an API

query parameters (/search?query=123) are supported:

// search.js
export default function({queryParams}){
  const results = someFancySearchProcess(queryParams.query)
  return {results}
}

GETs, POSTs, PUTs, and so on are supported, with JSON- or url-form-encoded request bodies:

// api/an_endpoint/index.js

export function get(){
  return "You got what you get"
}

export function post({requestBody}){
  return {
    "you posted this to me": requestBody
  }
}

export function put({requestBody}){
  return "I got a PUT, but honestly didn't do much with what you sent me"
}

Readme

Keywords

Package Sidebar

Install

npm i snz

Weekly Downloads

42

Version

0.1.1

License

MIT

Unpacked Size

76.7 kB

Total Files

59

Last publish

Collaborators

  • moredip