LiteNode's logo

LiteNode

Docs GitHub logo
▶ Usage

Documentation

Adding Routes

You can add routes for different HTTP methods (GET, POST, PUT, DELETE, PATCH):

Get

Signature:

get(routePath: string, ...handlers: RouteHandler[]): LiteNode

Defines a route with the GET method.

Example:

// Define a GET route with a route handler function
app.get("/users", (req, res) => {
    res.end("List of users")
})

// Define a GET route with middleware and a route handler function
app.get("/users/:id", authenticate, (req, res) => {
    const userId = req.params.id
    res.end(`User ID: ${userId}`)
})

Post

Signature:

post(routePath: string, ...handlers: (RouteHandler | number)[]): LiteNode

Defines a route with the POST method.

Example:

// Define a POST route with a route handler function
app.post("/users", (req, res) => {
    // The parsed body
    const data = req.body
    // Logic to create a new user
    res.end("User created successfully")
})

// Access the parsed body directly
app.post(
    "/users",
    (req, res, data) => {
        // 'data' represents the parsed body of the request
        // You can now use 'data' directly in your code
    },
    2 // Set a maximum request size limit of 2 MB (optional, default is 1MB)
)

For detailed documentation about the post method, including complete examples, please refer to the Body Parsing page.


Put

Signature:

put(routePath: string, ...handlers: RouteHandler[]): LiteNode

Defines a route with the PUT method.

Example:

// Define a PUT route with a route handler function
app.put("/users/:id", (req, res) => {
    const userId = req.params.id
    // Logic to update user with ID userId
    res.end(`User with ID ${userId} updated successfully`)
})

Delete

Signature:

delete(routePath: string, ...handlers: RouteHandler[]): LiteNode

Defines a route with the DELETE method.

Example:

// Define a DELETE route with a route handler function
app.delete("/users/:id", (req, res) => {
    const userId = req.params.id
    // Logic to delete user with ID userId
    res.end(`User with ID ${userId} deleted successfully`)
})

Patch

Signature:

patch(routePath: string, ...handlers: RouteHandler[]): LiteNode

Defines a route with the PATCH method.

Example:

// Define a PATCH route with a route handler function
app.patch("/users/:id", (req, res) => {
    const userId = req.params.id
    // Logic to update specific fields of user with ID userId
    res.end(`User with ID ${userId} patched successfully`)
})

Redirect

Signature:

redirect(location: string, statusCode: number = 302): void

Initiates a redirect response to the client with the specified location URL and optional status code. If status code is not provided, it defaults to 302 (temporary redirect). This method sets the appropriate HTTP headers for the redirect response and ends the response.

You can use the redirect() method in your route handlers to make a temporary or permanent redirect.

Example:

// Route to handle redirecting to the root URL
app.get("/redirect", (req, res) => {
    res.redirect("/") // Redirect to the root URL
})

// Route to handle permanent redirect to the root URL
app.get("/redirect-permanently", (req, res) => {
    res.redirect("/", 301) // Permanent redirect (301) to the root URL
})

Status

Signature:

status(code: number): nativeRes

Set the HTTP status code and send a response in a fluent, chainable manner within your route handlers.

The method chaining ensures that the response object (nativeRes) is returned, allowing for subsequent method calls such as json or render.

Example:

// Create a new LiteNode application instance
const app = new LiteNode()

// Route to handle POST requests to "/api/data"
app.post(
    "/api/data",
    async (req, res, data) => {
        // Process data...
        res.status(200).json({ success: true, data }) // Respond with a JSON success message and the processed data
    },
    0.25 // Set maximum request size to 0.25MB = 250KB
)

// Route to handle GET requests to "/template"
app.get("/template", (req, res) => {
    // Render the "template.html" file with the provided data
    res.status(200).render("template.html", { title: "Hello, World!" })
})

// Start the server on port 8080
app.startServer(8080)

Content