cpeak 1.4.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,331 @@
1
+ # Cpeak
2
+
3
+ Cpeak is a minimal and fast Node.js framework inspired by Express.js.
4
+
5
+ This project is designed to be improved until it's ready for use in complex production applications, aiming to be more performant and minimal than Express.js. This framework is intended for HTTP applications that primarily deal with JSON and file-based message bodies.
6
+
7
+ This is an educational project that was started as part of the [Understanding Node.js: Core Concepts](https://www.udemy.com/course/understanding-nodejs-core-concepts/?referralCode=0BC21AC4DD6958AE6A95) course. If you want to learn how to build a framework like this, and get to a point where you can build things like this yourself, check out this course!
8
+
9
+ ## Why Cpeak?
10
+
11
+ - **Minimalism**: No unnecessary bloat, with zero dependencies. Just the core essentials you need to build fast and reliable applications.
12
+ - **Performance**: Engineered to be fast, **Cpeak** won’t sacrifice speed for excessive customizability.
13
+ - **Educational**: Every new change made in the project will be explained in great detail in a YouTube playlist (playlist will be added soon). Follow this project and let's see what it takes to build an industry-leading product!
14
+ - **Express.js Compatible**: You can easily refactor from Cpeak to Express.js and vice versa. Many npm packages that work with Express.js will also work with Cpeak.
15
+
16
+ ## Table of Contents
17
+
18
+ - [Getting Started](#getting-started)
19
+ - [Hello World App](#hello-world-app)
20
+ - [Documentation](#documentation)
21
+ - [Including](#including)
22
+ - [Initializing](#initializing)
23
+ - [Middleware](#middleware)
24
+ - [Route Handling](#route-handling)
25
+ - [URL Variables & Parameters](#url-variables--parameters)
26
+ - [Sending Files](#sending-files)
27
+ - [Error Handling](#error-handling)
28
+ - [Listening](#listening)
29
+ - [Util Functions](#util-functions)
30
+ - [serveStatic](#servestatic)
31
+ - [parseJSON](#parsejson)
32
+ - [Complete Example](#complete-example)
33
+
34
+ ## Getting Started
35
+
36
+ Ready to dive in? Install **Cpeak** via npm:
37
+
38
+ ```bash
39
+ npm install cpeak
40
+ ```
41
+
42
+ ### Hello World App:
43
+
44
+ ```javascript
45
+ import cpeak from "cpeak";
46
+
47
+ const server = new cpeak();
48
+
49
+ server.route("get", "/", (req, res) => {
50
+ res.json({ message: "Hi there!" });
51
+ });
52
+
53
+ server.listen(3000, () => {
54
+ console.log("Server has started on port 3000");
55
+ });
56
+ ```
57
+
58
+ ## Documentation
59
+
60
+ ### Including
61
+
62
+ Include the framework like this:
63
+
64
+ ```javascript
65
+ import cpeak from "cpeak";
66
+ ```
67
+
68
+ Because of the minimalistic philosophy, you won’t add unnecessary objects to your memory as soon as you include the framework. If at any point you want to use a particular utility function (like `parseJSON` and `serveStatic`), include it like the line below, and only at that point will it be moved into memory:
69
+
70
+ ```javascript
71
+ import cpeak, { serveStatic, parseJSON } from "cpeak";
72
+ ```
73
+
74
+ ### Initializing
75
+
76
+ Initialize the Cpeak server like this:
77
+
78
+ ```javascript
79
+ const server = new cpeak();
80
+ ```
81
+
82
+ Now you can use this server object to start listening, add route logic, add middleware functions, and handle errors.
83
+
84
+ ### Middleware
85
+
86
+ If you add a middleware function, that function will run before your route logic kicks in. Here you can customize the request object, return an error, or do anything else you want to do prior to your route logic, like authentication.
87
+
88
+ After calling `next`, the next middleware function is going to run if there’s any; otherwise, the route logic is going to run.
89
+
90
+ ```javascript
91
+ server.beforeEach((req, res, next) => {
92
+ if (req.headers.authentication) {
93
+ // Your authentication logic...
94
+ req.userId = "<something>";
95
+ req.custom = "This is some string";
96
+ next();
97
+ } else {
98
+ // Return an error and close the request...
99
+ return res.status(401).json({ error: "Unauthorized" });
100
+ }
101
+ });
102
+
103
+ server.beforeEach((req, res, next) => {
104
+ console.log(
105
+ "The custom value was added from the previous middleware: ",
106
+ req.custom
107
+ );
108
+ next();
109
+ });
110
+ ```
111
+
112
+ ### Route Handling
113
+
114
+ You can add new routes like this:
115
+
116
+ ```javascript
117
+ server.route("patch", "/the-path-you-want", (req, res) => {
118
+ // your route logic
119
+ });
120
+ ```
121
+
122
+ First add the HTTP method name you want to handle, then the path, and finally, the callback. The `req` and `res` object types are the same as in the Node.js HTTP module (`http.IncomingMessage` and `http.ServerResponse`). You can read more about them in the [official Node.js documentation](https://nodejs.org/docs/latest/api/http.html).
123
+
124
+ ### URL Variables & Parameters
125
+
126
+ Since in HTTP these are called URL parameters: `/path?key1=value1&key2=value2&foo=900`, in Cpeak, we also call them `params` (short for HTTP URL parameters).
127
+ We can also do custom path management, and we call them `vars` (short for URL variables).
128
+
129
+ Here’s how we can read both:
130
+
131
+ ```javascript
132
+ // Imagine request URL is example.com/test/my-title/more-text?filter=newest
133
+ server.route("patch", "/test/:title/more-text", (req, res) => {
134
+ const title = req.vars.title;
135
+ const filter = req.params.get("filter");
136
+
137
+ console.log(title); // my-title
138
+ console.log(filter); // newest
139
+ });
140
+ ```
141
+
142
+ ### Sending Files
143
+
144
+ You can send a file as a Node.js Stream anywhere in your route or middleware logic like this:
145
+
146
+ ```javascript
147
+ server.route("get", "/testing", (req, res) => {
148
+ return res.status(200).sendFile("<file-path>", "<mime-type>");
149
+
150
+ // Example:
151
+ // return res.status(200).sendFile("./images/sun.jpeg", "image/jpeg");
152
+ });
153
+ ```
154
+
155
+ The file’s binary content will be in the HTTP response body content. Make sure you specify a correct path relative to your CWD (use the `path` module for better compatibility) and also the correct HTTP MIME type for that file.
156
+
157
+ ### Error Handling
158
+
159
+ If anywhere in your route functions you want to return an error, it's cleaner to pass it to the `handleErr` function like this:
160
+
161
+ ```javascript
162
+ server.route("get", "/api/document/:title", (req, res, handleErr) => {
163
+ const title = req.vars.title;
164
+
165
+ if (title.length > 500)
166
+ return handleErr({ status: 400, message: "Title too long." });
167
+
168
+ // The rest of your logic...
169
+ });
170
+ ```
171
+
172
+ And then handle all the errors like this in the `handleErr` callback:
173
+
174
+ ```javascript
175
+ server.handleErr((error, req, res) => {
176
+ if (error && error.status) {
177
+ res.status(error.status).json({ error: error.message });
178
+ } else {
179
+ // Log the unexpected errors somewhere so you can keep track of them...
180
+ console.error(error);
181
+ res.status(500).json({
182
+ error: "Sorry, something unexpected happened on our side.",
183
+ });
184
+ }
185
+ });
186
+ ```
187
+
188
+ The error object is the object that you passed to the `handleErr` function earlier in your routes.
189
+
190
+ ### Listening
191
+
192
+ Start listening on a specific port like this:
193
+
194
+ ```javascript
195
+ server.listen(3000, () => {
196
+ console.log("Server has started on port 3000");
197
+ });
198
+ ```
199
+
200
+ ### Util Functions
201
+
202
+ There are utility functions that you can include and use as middleware functions. These are meant to make it easier for you to build HTTP applications. In the future, many more will be added, and you only move them into memory once you include them. No need to have many npm dependencies for simple applications!
203
+
204
+ The list of utility functions as of now:
205
+
206
+ - serveStatic
207
+ - parseJSON
208
+
209
+ Including any one of them is done like this:
210
+
211
+ ```javascript
212
+ import cpeak, { utilName } from "cpeak";
213
+ ```
214
+
215
+ #### serveStatic
216
+
217
+ With this middleware function, you can automatically set a folder in your project to be served by Cpeak. Here’s how to set it up:
218
+
219
+ ```javascript
220
+ server.beforeEach(
221
+ serveStatic("./public", {
222
+ mp3: "audio/mpeg",
223
+ })
224
+ );
225
+ ```
226
+
227
+ If you have file types in your public folder that are not one of the following, make sure to add the MIME types manually as the second argument in the function as an object where each property key is the file extension, and each value is the correct MIME type for that. You can see all the available MIME types on the [IANA website](https://www.iana.org/assignments/media-types/media-types.xhtml).
228
+
229
+ ```
230
+ html: "text/html",
231
+ css: "text/css",
232
+ js: "application/javascript",
233
+ jpg: "image/jpeg",
234
+ jpeg: "image/jpeg",
235
+ png: "image/png",
236
+ svg: "image/svg+xml",
237
+ txt: "text/plain",
238
+ eot: "application/vnd.ms-fontobject",
239
+ otf: "font/otf",
240
+ ttf: "font/ttf",
241
+ woff: "font/woff",
242
+ woff2: "font/woff2"
243
+ ```
244
+
245
+ #### parseJSON
246
+
247
+ With this middleware function, you can easily read and send JSON in HTTP message bodies in route and middleware functions. Fire it up like this:
248
+
249
+ ```javascript
250
+ server.beforeEach(parseJSON);
251
+ ```
252
+
253
+ Read and send JSON from HTTP messages like this:
254
+
255
+ ```javascript
256
+ server.route("put", "/api/user", (req, res) => {
257
+ // Reading JSON from the HTTP request:
258
+ const email = req.body.email;
259
+
260
+ // rest of your logic...
261
+
262
+ // Sending JSON in the HTTP response:
263
+ res.status(201).json({ message: "Something was created..." });
264
+ });
265
+ ```
266
+
267
+ ## Complete Example
268
+
269
+ Here you can see all the features that Cpeak offers, in one small piece of code:
270
+
271
+ ```javascript
272
+ import cpeak, { serveStatic, parseJSON } from "cpeak";
273
+
274
+ const server = new cpeak();
275
+
276
+ server.beforeEach(
277
+ serveStatic("./public", {
278
+ mp3: "audio/mpeg",
279
+ })
280
+ );
281
+
282
+ // For parsing JSON bodies
283
+ server.beforeEach(parseJSON);
284
+
285
+ // Adding custom middleware functions
286
+ server.beforeEach((req, res, next) => {
287
+ req.custom = "This is some string";
288
+ next();
289
+ });
290
+
291
+ // Adding route handlers
292
+ server.route("get", "/api/document/:title", (req, res, handleErr) => {
293
+ // Reading URL variables
294
+ const title = req.vars.title;
295
+
296
+ // Reading URL parameters (like /users?filter=active)
297
+ const filter = req.params.get("filter");
298
+
299
+ // Reading JSON request body
300
+ const anything = req.body.anything;
301
+
302
+ // Handling errors
303
+ if (anything === "not-expected-thing")
304
+ return handleErr({ status: 400, message: "Invalid property." });
305
+
306
+ // Sending a JSON response
307
+ res.status(200).json({ message: "This is a test response" });
308
+ });
309
+
310
+ // Sending a file response
311
+ server.route("get", "/file", (req, res) => {
312
+ // Make sure to specify a correct path and MIME type...
313
+ res.status(200).sendFile("<path-to-file-relative-to-cwd>", "<mime-type>");
314
+ });
315
+
316
+ // Handle all the errors that could happen in the routes
317
+ server.handleErr((error, req, res) => {
318
+ if (error && error.status) {
319
+ res.status(error.status).json({ error: error.message });
320
+ } else {
321
+ console.error(error);
322
+ res.status(500).json({
323
+ error: "Sorry, something unexpected happened from our side.",
324
+ });
325
+ }
326
+ });
327
+
328
+ server.listen(3000, () => {
329
+ console.log("Server has started on port 3000");
330
+ });
331
+ ```
package/lib/index.js CHANGED
@@ -1,8 +1,9 @@
1
- const http = require("node:http");
2
- const fs = require("node:fs/promises");
1
+ import http from "node:http";
2
+ import fs from "node:fs/promises";
3
3
 
4
- const { parseJSON, serveStatic } = require("./util");
5
- class CPeak {
4
+ import { serveStatic, parseJSON } from "./util.js";
5
+
6
+ class Cpeak {
6
7
  constructor() {
7
8
  this.server = http.createServer();
8
9
  this.routes = {};
@@ -33,6 +34,7 @@ class CPeak {
33
34
  res.end(JSON.stringify(data));
34
35
  };
35
36
 
37
+ // Parse the URL parameters (like /users?name=John)
36
38
  const urlWithoutParams = req.url.split("?")[0];
37
39
  req.params = new URLSearchParams(req.url.split("?")[1]);
38
40
 
@@ -40,21 +42,25 @@ class CPeak {
40
42
  const runMiddleware = (req, res, middleware, index) => {
41
43
  // Out exit point...
42
44
  if (index === middleware.length) {
43
- // If the routes object does not have a key of req.method + req.url, return 404
44
- if (!this.routes[req.method.toLocaleLowerCase() + urlWithoutParams]) {
45
- return res
46
- .status(404)
47
- .json({ error: `Cannot ${req.method} ${urlWithoutParams}` });
45
+ for (const route of this.routes[req.method.toLowerCase()]) {
46
+ const match = urlWithoutParams.match(route.regex);
47
+
48
+ if (match) {
49
+ // Parse the URL variables from the matched route (like /users/:id)
50
+ const vars = this.#extractVars(route.path, match);
51
+ // Call the route handler with request and URL variables
52
+ req.vars = vars;
53
+ return route.cb(req, res, (error) => {
54
+ res.setHeader("Connection", "close");
55
+ this.handleErr(error, req, res);
56
+ });
57
+ }
48
58
  }
49
59
 
50
- this.routes[req.method.toLowerCase() + urlWithoutParams](
51
- req,
52
- res,
53
- (error) => {
54
- res.setHeader("Connection", "close");
55
- this.handleErr(error, req, res);
56
- }
57
- );
60
+ // If the requested route dose not exist, return 404
61
+ return res
62
+ .status(404)
63
+ .json({ error: `Cannot ${req.method} ${urlWithoutParams}` });
58
64
  } else {
59
65
  middleware[index](req, res, () => {
60
66
  runMiddleware(req, res, middleware, index + 1);
@@ -67,7 +73,10 @@ class CPeak {
67
73
  }
68
74
 
69
75
  route(method, path, cb) {
70
- this.routes[method + path] = cb;
76
+ if (!this.routes[method]) this.routes[method] = [];
77
+
78
+ const regex = this.#pathToRegex(path);
79
+ this.routes[method].push({ path, regex, cb });
71
80
  }
72
81
 
73
82
  beforeEach(cb) {
@@ -83,9 +92,37 @@ class CPeak {
83
92
  cb();
84
93
  });
85
94
  }
95
+
96
+ // ------------------------------
97
+ // PRIVATE METHODS:
98
+ // ------------------------------
99
+ #pathToRegex(path) {
100
+ const varNames = [];
101
+ const regexString =
102
+ "^" +
103
+ path.replace(/:\w+/g, (match, offset) => {
104
+ varNames.push(match.slice(1));
105
+ return "([^/]+)";
106
+ }) +
107
+ "$";
108
+
109
+ const regex = new RegExp(regexString);
110
+ return regex;
111
+ }
112
+
113
+ #extractVars(path, match) {
114
+ // Extract url variable values from the matched route
115
+ const varNames = (path.match(/:\w+/g) || []).map((varParam) =>
116
+ varParam.slice(1)
117
+ );
118
+ const vars = {};
119
+ varNames.forEach((name, index) => {
120
+ vars[name] = match[index + 1];
121
+ });
122
+ return vars;
123
+ }
86
124
  }
87
125
 
88
- CPeak.parseJSON = parseJSON;
89
- CPeak.serveStatic = serveStatic;
126
+ export { serveStatic, parseJSON };
90
127
 
91
- module.exports = CPeak;
128
+ export default Cpeak;
package/lib/util.js CHANGED
@@ -1,5 +1,5 @@
1
- const fs = require("node:fs");
2
- const path = require("node:path");
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
3
 
4
4
  const MIME_TYPES = {
5
5
  html: "text/html",
@@ -17,7 +17,7 @@ const MIME_TYPES = {
17
17
  woff2: "font/woff2",
18
18
  };
19
19
 
20
- exports.serveStatic = (folderPath, newMimeTypes) => {
20
+ const serveStatic = (folderPath, newMimeTypes) => {
21
21
  // For new user defined mime types
22
22
  if (newMimeTypes) {
23
23
  Object.assign(MIME_TYPES, newMimeTypes);
@@ -75,7 +75,7 @@ exports.serveStatic = (folderPath, newMimeTypes) => {
75
75
  };
76
76
 
77
77
  // Parsing JSON
78
- exports.parseJSON = (req, res, next) => {
78
+ const parseJSON = (req, res, next) => {
79
79
  // This is only good for bodies that their size is less than the highWaterMark value
80
80
  if (req.headers["content-type"] === "application/json") {
81
81
  let body = "";
@@ -92,3 +92,5 @@ exports.parseJSON = (req, res, next) => {
92
92
  next();
93
93
  }
94
94
  };
95
+
96
+ export { serveStatic, parseJSON };
package/package.json CHANGED
@@ -1,11 +1,26 @@
1
1
  {
2
2
  "name": "cpeak",
3
- "version": "1.4.0",
4
- "description": "",
3
+ "version": "2.1.0",
4
+ "description": "A minimal and fast Node.js HTTP framework.",
5
+ "type": "module",
5
6
  "main": "./lib/index.js",
6
7
  "scripts": {
7
8
  "test": "echo \"Error: no test specified\" && exit 1"
8
9
  },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/agile8118/cpeak.git"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/agile8118/cpeak/issues"
16
+ },
17
+ "homepage": "https://github.com/agile8118/cpeak#readme",
9
18
  "author": "Cododev Technology",
10
- "license": "ISC"
19
+ "license": "MIT",
20
+ "keywords": [
21
+ "cpeak",
22
+ "nodejs",
23
+ "http",
24
+ "framework"
25
+ ]
11
26
  }