Skip to content

Commit

Permalink
Merge pull request #72 from IntegerAlex/params
Browse files Browse the repository at this point in the history
Added params #69
  • Loading branch information
IntegerAlex authored Oct 18, 2024
2 parents c4846fb + d10f33f commit 4ce7872
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 14 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Changelog

All notable changes to this project will be documented in this file.
## [0.8.0] - 2024-10-10
### Fixed
- Fixed the issue of `res.download()` not sending the correct response.

### Added
- Added support for `cors` middleware to enable Cross-Origin Resource Sharing.
- which is in built by using `server.cors(true)` to enable cors.
- Added support for `params` in routes. Now you can define routes with parameters like `/users/:id` and access the parameter value using `req.params.id`.
- Added `download()` method to the response object `res.download()`.

## [0.6.0] - 2024-10-09
### Fixed
- HTTPParserError: Response does not match the HTTP/1.1 protocol (Invalid header value char)
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Hasty


![NPM Version](https://img.shields.io/npm/v/hasty-server)
![NPM Downloads](https://img.shields.io/npm/d18m/hasty-server)
![NPM License](https://img.shields.io/npm/l/hasty-server)
Expand Down Expand Up @@ -84,9 +83,9 @@ If you would like to contribute to Hasty Server, you're welcome to:
Note: Do not use third-party code or dependencies. You can take help from language models, but avoid directly copying any of their code.

### CHANGELOG
- v0.6.0
- Added `sendFile()` method to send static files.
- Added `server.close()` method to close the server explicitly.
- v0.8.0
- Added `download()` method to send file as an attachment.
- Added `server.cors(true)` to enable `cors`.

For more information, see .
[CHANGELOG](CHANGELOG.md)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hasty-server",
"version": "0.7.1",
"version": "0.8.0",
"main": "./server/index.js",
"directories": {
"lib": "lib",
Expand Down Expand Up @@ -32,7 +32,7 @@
"type": "git",
"url": "https://github.com/IntegerAlex/hasty-server.git"
},
"homepage": "https://hasty-server.vercel.app",
"homepage": "https://hasty-server.vercel.app",
"license": "GPL-3.0",
"description": "A Blazing fast simple http server for node.js"
}
72 changes: 64 additions & 8 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,76 @@ function handler(socket,context){

}

function pathController(data, context, res) {
const path = data.path;
const method = data.method;

console.log("Received path: " + path + ", Method: " + method);

function pathController(data,context, socket) {
const path = data.path;
const method = data.method
console.log("pathController: " + method + ": " + path);
// Find the matching route, accounting for parameters
const route = context.routes.find(route => {
return matchRouteWithParams(route.path, path) && route.method === method;
});

if (route) {
console.log("Route matched: " + route.path);

// Extract parameters from the matched route
const params = extractParams(route.path, path);
console.log("Extracted params:", params); // Log extracted params

data.params = params; // Attach extracted params to data
route.callback(data, res); // Pass the updated data with params
} else {
console.log("No route matched");
res.sendStatus(404); // Route not found
}
}

// Check if the path exists in the context.routes
const route = context.routes.find(route => route.path === path && route.method === method);
if (route) route.callback(data, socket);
else socket.sendStatus(404);
// Helper function to check if the route matches, including parameters
function matchRouteWithParams(routePath, actualPath) {
const routeParts = routePath.split('/');
const pathParts = actualPath.split('/');

if (routeParts.length !== pathParts.length) return false;

return routeParts.every((part, index) => {
return part.startsWith(':') || part === pathParts[index];
});
}

// Helper function to extract params from the matched route
function extractParams(routePath, actualPath) {
const routeParts = routePath.split('/');
const pathParts = actualPath.split('/');
const params = {};

routeParts.forEach((part, index) => {
if (part.startsWith(':')) {
const paramName = part.slice(1); // Remove the colon (:) from parameter name
params[paramName] = pathParts[index]; // Assign actual path value
}
});

return params;
}


//function pathController(data,context, socket) {
// const path = data.path;
// const method = data.method
// console.log("pathController: " + method + ": " + path);
//
//
//
//
// // Check if the path exists in the context.routes
// const route = context.routes.find(route => route.path === path && route.method === method);
// if (route) route.callback(data, socket);
// else socket.sendStatus(404);
//}
//


class Server {
socket;
Expand Down

0 comments on commit 4ce7872

Please sign in to comment.