@shisakankoy/fast-food
v0.1.3
Published
FastFood is a lightweight HTTP framework for Node.js.
Maintainers
Readme
FastFood
FastFood is a lightweight HTTP framework for Node.js.
It is designed to make creating HTTP servers easier while staying close to the native Node.js http module.
Features
- Lightweight
- Simple routing with
get()andpost() - Simple response helpers
- URL query parsing with
req.query - Form body parsing with
req.bodyParams - Built on the native Node.js
httpmodule - Easy to learn and use
Installation
npm install @shisakankoy/fast-foodExample
import { FastFood } from "@shisakankoy/fast-food";
const app = new FastFood();
app.listen(3000, "0.0.0.0", () => {
console.log("Server started");
});
// GET
app.get("/", (req, res) => {
res.header(200, {
"Content-Type": "text/plain"
});
res.print("Hello, FastFood!");
res.end();
});
// POST
app.post("/data", (req, res) => {
req.at("in");
const number = req.query.get("number");
req.at("end", () => {
const message = req.bodyParams.get("msg");
console.log("Received:", req.body);
console.log("Message:", message);
console.log("Number:", number);
res.header(200, {
"Content-Type": "text/plain"
});
res.print("Data received.");
res.end();
});
});Syntax
Start the server
app.listen(port, host, handler);Register a GET route
app.get(path, (req, res) => {});Register a POST route
app.post(path, (req, res) => {});Set response headers
res.header(statusCode, headers);- Equivalent to
res.writeHead().
Write a response
res.print(text);- Equivalent to
res.write().
Receive POST data
req.at("in");- A shorthand for
req.on("data").
req.at("end", handler);- A shorthand for
req.on("end").
Read URL query parameters
req.query- A
URLSearchParamsobject created from the URL query string.
Example:
const id = req.query.get("id");Read form body parameters
req.bodyParams- A
URLSearchParamsobject created from the request body. - Available only inside
req.at("end").
Example:
const message = req.bodyParams.get("msg");License
MIT License
Notes
- Call
listen()before registering routes withget()orpost(). - The
listen()method requires a host name (for example,"0.0.0.0"or"127.0.0.1"). req.bodyParamsis available only insidereq.at("end").
