1.1.7 • Published 4 years ago

spin-server v1.1.7

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

spin-server converts Functions and Modules to APIs in one line of code!

It preserves the function and variable names and returns a 200 status if there is no error and a 500 if there is an error.

#Installation:

npm install spin-server

#Example:

var spin = require('spin-server')
function hello ( name ) { 	
    return `Hello ${name}!`
}
spin(hello,3000)

This creates an Express Endpoint "/hello" on port 3000 that wants a variable called "name" in the POST or GET data.

Example curl requests:

curl localhost:3000/hello?name=Ryan
Hello Ryan!

curl -d "name=Ryan" localhost:3000/hello
Hello Ryan!

#Anonymous functions:

You can spin anonymous functions up to "/"

spin( function ( name ) { 
    return `Hello ${name}!` 
} , 3000 )

And then send a request to them

curl localhost:3000/?name=Ryan
Hello Ryan!

curl -d "name=Ryan" localhost:3000/
Hello Ryan!

#Modules and Objects:

You can also spin up an object of Functions or a Module

So if you had a file called hello.js

function hello ( name ) { 
    return `Hello ${name}!`
}
function hi ( name ) {
    return `Hi ${name}!`
}

module.exports = { hello, hi }

And then in another file you spun it up

var hello = require('./hello.js')
spin ( hello , 3000 )

You could use the endpoints

 curl -d "name=Ryan" localhost:3000/hello
 Hello Ryan!

 curl localhost:3000/hi?name=Ryan
 Hi Ryan!

#Sessions:

If you need to use session data, simply use "$" in the arguments

function hello ( name, $ ) {
    $.name = name
    return `Hello ${name}!`
}
function who ( $ ) {
    return `You are ${$.name}!`
}
function bye ( $ ) {
    var name = $.name
    delete($.name)
    return `Bye ${name}!`
}

module.exports = { hello, bye }

And then send up some commands

curl localhost:3000/hello?name=Ryan
Hello Ryan!

curl localhost:3000/who
You are Ryan!

curl loalhost:3000/bye
Bye Ryan!

#Errors:

If the function throws an error, the error message is forwarded along with a 500 status

function sorry ( ) { 
    throw `I'm sorry!`
}
function ok () {
    return "okay"
}
spin ( { sorry , ok } , 3000)

Then test it

curl localhost:3000/sorry
I'm sorry!

curl -o /dev/null -s -w "%{http_code}\n" http://localhost:3000/sorry
500

curl localhost:3000/ok
okay

curl -o /dev/null -s -w "%{http_code}\n" http://localhost:3000/ok
200

#Redirects:

If you need to use a redirect, include it with "redirect" as an argument just like you would with session. "redirect" is a function that tells spin-server to redirect to the URL you pass as an argument

function home ( redirect ) {
return redirect ( "/index.html" )
}
spin(home, 3000)

And then call it

curl localhost:3000/home
Found. Redirecting to /index.html

curl -o /dev/null -s -w "%{http_code}\n" http://localhost:3000/home
302

Enjoy!

And if you want to start selling your API in 5 minutes, list it on API Exchange!

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.0.4

4 years ago

1.1.2

4 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago