This is a simple nodejs server, it has zero dependancy!
- [x] zero dependancy
- [x] basic route handler
- [x] serve static files
- [x] read request query string and post body
- [x] basic cookies and session
- [x] basic template enggine
- [x] Handle POST Body for both JSON, multipart, and url-encoded
- nvm
- nodejs >= v20.16.0
- yarn
git clone https://github.com/deckyfx/sea-builder.git
Make sure nvm is installed, and install node version 20.16.0
, visit https://github.com/nvm-sh/nvm for more detail
Set node version
nvm use
Check node version
node --version
it should return
v20.17.0
Make sure yarn is installed, if not use
npm install --global yarn
Install dependecies
yarn install
1.0.6
Install with
npm i @decky.fx/node-server
See example folder
import { server, any, onError, get, post, routes } from "../src/index";
const hostname = "0.0.0.0";
const port = 3000;
any("/", async function _any(handle) {
handle.json({ result: "OK" });
return true;
});
post("/test", async function _post(handle) {
handle.json({ result: "OK" });
return true;
});
get("/html", async function _html(handle) {
handle.html(handle.readFile("assets", "public", "file.html"));
return true;
});
onError(async function _error(handle) {
const { req, res, ...data } = handle;
console.log(data);
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
The server object created by node:http
Add GET route
Add POST route
Add PUT route
Add DELETE route
Add OPTIONS route
Add route catching any methods
Add route for method xxx
Flag the server as up
Flag the server as down for maintenance
Flag CORS enabled / disabled, if enabled
, response will send extra headers for CORS handler, and when adding POST route will also adds OPTIONS route to same path to handle preflight request
Set public dir to serve static files, default
is ./public
Set default upload dir, default
is ./uploads
Add hooks when error happened during request route parsing
Add hooks that trigger before a default File handler triggered
Add hooks that trigger before a default Index handler triggered
Return current configured routes
Render template and generate string data of rendered template
Send basic string to connected client
Send basic string of JSON stringified data to connected client
Send static file contents to connected client
Redirect client to new path
The callback that called when a route is handled, starting from 1.0.5
, the callback only provide single argument, a HTTPHandler
instance
The Handler data passed when trigger RouteHandler
.
The Handler would have the following readonly
properties:
- cookies?: Cookie
- error?: Error
- method: RequestMethod
- path: string
- path_data?: Record<string, any>
- qs?: Record<string, any>
- session?: Session
- status: number
- type: RequestType
- req: http.IncomingMessage
- res: http.ServerResponse
The Handler would have the following methods:
Send and end request with text
Send and end request with json data
Send and end request with html string data
Send and end request with redirect header
Send and end request with error
Send headers for CORS
Process a template file and return the string result
Read file as string data
Send file response, as file download when disposition
= true
, or as common HTTP file response (eg: js, css, image)
Cookie instance has the following methods
Return all cookies
Set a cookie
remove
clear all cookies
Session instance has the following methods
Return all sessions
Set a session
remove a session
clear all sessions
- [ ] Next idea