Skip to content

A simple and easy web framework for nodejs πŸš€

Notifications You must be signed in to change notification settings

prnv404/fastnet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

74 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Commitizen friendly CI jest ESLint Husky TypeScript Open Source? Yes!

FastNet - A Minimalistic Node.js Web Framework

FastNet is a lightweight and easy-to-use Node.js web framework designed to be very minimal. With a focus on simplicity, FastNet APIs have the same signature as Express.

Features

  • Simplicity: FastNet is designed to be easy to use and understand
  • Routing: Only In app routing feature.
  • Middleware Support: Easily integrate middleware functions for customizing request and response handling.
  • JSON Body Parsing: Parse JSON request bodies with built-in support for handling incoming data.
  • HTTP Methods: Support for common HTTP methods including GET, POST, PUT, and DELETE.
  • Flexibility: It easy to modifiy because its very minimal.

Installation

To get started with FastNet, install it using npm:

npm install fastnet

Usage

// Import FastNet and necessary types
import FastNet, { Request, Response } from 'fastnet';

// Create an instance of FastNet
const app = new FastNet();

// Define a sample route
app.get('/hello', (req: Request, res: Response) => {
  res.send('Hello, FastNet!');
});

// Start the server
const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server is running on https://localhost:${PORT}`);
});

Reference


Request Object

The Request object is designed to encapsulate and provide convenient access to properties and methods of an HTTP request represented by the IncomingMessage class from the "http" module. This documentation outlines the various functionalities provided by the Request class.

Constructor

Request(req: IncomingMessage)

Creates a new Request instance with the provided IncomingMessage object representing the HTTP request.

Properties

header

  • Description: Gets or sets the headers of the request.
  • Getter: Returns the headers property of the req object.
  • Setter: Sets the value of the headers property in the req object to the provided value.

url

  • Description: Gets the URL of the current request.
  • Getter: Returns the url property of the req object.

method

  • Description: Gets or sets the HTTP method of the request.
  • Getter: Returns the HTTP method of the request object or an empty string if not defined.
  • Setter: Sets the value of the "method" property in the req object to the provided value if it is a string.

body

  • Description: Asynchronously retrieves and parses the JSON body of an HTTP request.
  • Getter: Returns a Promise that resolves with the parsed JSON body.

path

  • Description: Gets or sets the pathname of the requested URL.
  • Getter: Returns the pathname property of the parsed URL from the req object or an empty string if not available.
  • Setter: Sets the path of a URL if it is different from the current path.

params

  • Description: Gets or sets the parameters of the request.
  • Getter: Returns the params property of the req object.
  • Setter: Sets the value of the params property in the req object to the provided value.

Response Object

The Response object is designed to encapsulate and provide convenient methods for handling the server's HTTP response represented by the ServerResponse class from the "http" module. This documentation outlines the various functionalities provided by the Response class.

Constructor

Response(res: ServerResponse)

Creates a new Response instance with the provided ServerResponse object representing the HTTP response.

Properties

socket

  • Description: Gets the underlying socket associated with the response.
  • Getter: Returns the socket property of the res object.

status

  • Description: Gets or sets the HTTP status code of the response.
  • Getter: Returns the status code of the response.
  • Setter: Sets the HTTP status code of the response to the provided value.

Methods

send(data: any)

  • Description: Sends data as the response body.
  • Parameters:
    • data (any): The data to be sent as the response body.
  • Behavior:
    • If data is an object, it will be converted to JSON and sent with the "application/json" content type.
    • If data is a string, it will be sent with the "text" content type.
    • The response headers are set accordingly.
    • The response is ended after sending the data.

Example of Todo Application

import FastNet, { Request, Response } from 'fastnet'

const app = new FastNet();

const PORT = 3000;

let todos = [
	{ id: 1, text: "Learn Node.js", done: false },
	{ id: 2, text: "Build a TODO app", done: false }
];

// Get all todos
app.get("/todos", async (req: Request, res: Response) => {
	res.send(todos);
});

// Get a specific todo by ID
app.get("/todos/:id", async (req: Request, res: Response) => {
	const todoId = parseInt(req.params.id);	
	const todo = todos.find((t) => t.id === todoId);

	if (todo) {
		res.send(todo);
	} else {
		res.send({ error: "Todo not found" });
	}
});

// Create a new todo
app.post("/todos", async (req: Request, res: Response) => {
	const { text } = (await req.body) as any;
	const newTodo = {
		id: todos.length + 1,
		text,
		done: false
	};
	todos.push(newTodo);
	res.send(newTodo);
});

// Update a todo
app.put("/todos/:id", async (req: Request, res: Response) => {
	const todoId = parseInt(req.params.id);
	const { text } = (await req.body) as any;
	const todo = todos.find((t) => t.id === todoId);

	if (todo) {
		todo.text = text;
		res.send(todo);
	} else {
		res.send({ error: "Todo not found" });
	}
});

// Delete a todo
app.delete("/todos/:id", (req: Request, res: Response) => {
	const todoId = parseInt(req.params.id);
	todos = todos.filter((t) => t.id !== todoId);
	res.send({ message: "Todo deleted successfully" });
});

app.listen(PORT, () => {
	console.log(`Server is running on https://localhost:${PORT}`);
});

Contributing

Contributions are welcome! There is a need for a lot of improvement to this because it is very minimal. Please open an issue or submit a pull request

About

A simple and easy web framework for nodejs πŸš€

Resources

Stars

Watchers

Forks

Packages

No packages published