Big-Damn-Heroes (BDH) makes writing your server, routes, and HTTP responses easier while adding some flavor for Firefly fans.
Assuming you've already installed Node.js, get started by using npm init
to create a package.json
file in your project directory:
$ npm init
To use this package, install BDH as a dependency:
$ npm install big-damn-heroes --save
Or install it temporarily and don't include it in your dependencies list:
$ npm install big-damn-heroes
In your project directory, create a file named bdh.js
and add the following code:
const bdh = require("big-damn-heroes");
bdh.get("/serenity", (req, res) => {
res.send("Shiny. Let's be bad guys.");
});
bdh.listen(3000);
BDH starts a server and listens on port 3000 for connections. This app will respond with an inspirational Firefly quote for requests to the root URL (/), and with Jayne's famous one-liner for GET requests to our /serenity
path. For every other path, it will respond with a default 404: Not Found message.
Note that the
req
(request) andres
(response) objects are the same ones that Node provides, so you can use them as you normally would without BDH involved.
Run your app with the following command:
$ node bdh.js
Then load http://localhost:3000 in your browser to see it in action. That's it! That's all it takes to get a simple server up and running with BDH.
Route definition takes the following structure:
bdh.METHOD(PATH, HANDLER)
Where:
- bdh is an instance of BDH.
- METHOD is an HTTP request method (GET, POST, PUT, PATCH, or DELETE only).
- PATH is a path on the server (valid string only).
- HANDLER is the function that is executed when the route is matched.
The following examples demonstrate how to define some simple routes:
Respond with a customized message on the homepage:
bdh.get("/", (req, res) => {
res.send("I aim to misbehave.");
});
Respond to a POST request on the /jayne
route:
bdh.post("/jayne", (req, res) => {
res.send("I'll be in my bunk.");
});
Respond to a DELETE request on the /wash
route:
bdh.delete("/wash", (req, res) => {
res.send("I'm a leaf on the wind.");
});
Create an instance of the BDH framework:
const bdh = require("big-damn-heroes");
Routes HTTP GET requests to the specified path with the provided callback function.
bdh.get("/kaylee", (req, res) => {
res.send("To hell with this! I'm gonna live!");
});
Routes HTTP POST requests to the specified path with the provided callback function.
Routes HTTP PUT requests to the specified path with the provided callback function.
Routes HTTP PATCH requests to the specified path with the provided callback function.
Routes HTTP DELETE requests to the specified path with the provided callback function.
Overrides the default 404: Not Found behavior when a callback function is provided:
bdh.badRoute((req, res) => {
res.send("Best be on your merry.");
});
Returns an instance of an HTTP server and starts it using the indicated port. It also prints a poignant message with the port information to the console, but this default behavior can be overridden by providing an optional callback function:
bdh.listen(3000, () => {
console.log("I swear by my pretty floral bonnet, I will end you.");
});
The res
object represents the HTTP response that BDH sends when it receives an HTTP request.
Sends a simple HTTP response.
The body
parameter can be a String
, an object, an Array
, or a Buffer
object representing a string. It automatically sets the Content-Type
header field based on the input, sets the appropriate status code, and ends the response. Objects and arrays are automatically JSON serialized. If no body
parameter is supplied, an empty string will be written to the response.