wooly.js v0.0.2
Wooly
Hi, wooly or wooly.js is a simple RESTful API framework, i guess. It's not finished yet, it has a lot of bugs (probably)
How to use it?
That's very simple, in your index.js
file just add:
const Wooly = require('wooly.js');
const server = new Wooly.Server({port: 8080, routingDir: './routes', staticDir: './public'});
Simple, isn't it? Now, what does each line?
const Wooly = require('wooly.js');
: Tell node that we want to use wooly.js
const server = new Wooly.Server()
: Creates a new server from the given properties
{port: 8080, routingDir: './routes', staticDir: './public'}
The properties of the server, it will be started at the port 8080, and the routes folder, every JS file inside it will be considered a route, and finally, the public folder, where you can serve your static files.
I made that, now, how do i make a route?
Create a new file in routes
with the name you want, it doesn't really matter.
Then, add this to your module.exports
:
name: "",
path: "/",
methodsAllowed: ["GET"],
async function handler(req, res)
{
res.send("Congrats! You made an 'api' with Wooly.js")
}
Now, what does each line?
name
: This will be the name of your route/endpoint, put the name between [] if you want it as a parameter.
path
: The base url of your route/endpoint, if it's /
, it will be the entry point, also known as index
, you can also pass parameters to it, put them between [] for that.
methodsAllowed
: This will be a limit for your API, it will only register the HTTP methods you put.
Now, if you run node .
, it will start your API in the given port, amazing, isn't it?
Events
I only made 3 events for now, though i dont know if they work, let me know if they dont, however, here are the events:
ready
: Fires when the server starts
routeRegistered
: Fires when the server register a route/endpoint
routeAliasRegistered
: Fires when the server registers an alias for a route/endpoint
Info
Did you know you can add aliases? What about HTML Responses?
Well, i also made these two, to integrate aliases, just add an array of strings to the module.exports
of your route/endpoint, like this:
aliases: ["alias1", "alias2"]
And for adding HTML Responses (very limited, use res.sendFile
instead), add these two things to the module.exports
of your route/endpoint:
htmlResponse: true,
htmlSettings:
{
head: "",
body: ""
}
That's it, hope you enjoy it :)
Let me know if you want me to add a feature