Node / TypeScript
Steps
Install Node
Install Node (v20+).
Install Dependencies
Install the dependencies in package.json.
npm installCreate the Following File
import {
Application,
Resource,
} from "@drashland/drash/modules/http.polyfill.js";
import type { RequestMethod } from "@drashland/drash/core/Types.js";
import { createServer, IncomingMessage, ServerResponse } from "node:http";
type NodeContext = { // The shape you hand the application.
url: string;
method: RequestMethod;
request: IncomingMessage;
response: ServerResponse;
};
class Home extends Resource { // Create a resource.
public paths = ["/"]; // Tell it which path(s) it answers to.
GET(context: NodeContext) { // Node hands you the context object.
console.log(`Received request: ${context.request.url}`);
context.response.end( // Write straight to Node's ServerResponse.
`Oh so easy (written at ${new Date()})`,
);
}
}
const app = Application
.builder() // Get the app's builder so we can build the app easily.
.resources(Home) // Add the `Home` resource to the app.
.build(); // Build the app.
const hostname = "localhost"; // Define server variables for reuse below.
const port = 1447;
const server = createServer((request, response) => {
// Node's `node:http` gives you `IncomingMessage` and `ServerResponse`, not
// a Web `Request`. Drash does not convert them for you — you hand the application
// a context object carrying whatever your resources need. The chain itself
// only requires `url` and `method`.
const context = {
url: `http://${hostname}:${port}${request.url}`,
method: request.method,
request,
response,
};
return app // Let the app
.handle(context) // handle the context object, and
.catch((error) => { // catch anything it throws.
if (context.url.includes("favicon")) {
return response.end(); // Browsers ask for this; ignore it.
}
console.log(`Request URL hit an error: ${context.url}:\n`);
console.log({ error });
response.statusCode = 500; // Everything else gets a 500.
response.statusMessage = "Internal Server Error";
response.end("Sorry, but we hit an error!");
});
});
// Start the server.
server.listen(port, hostname, () => {
console.log(`\nDrash running at http://${hostname}:${port}`);
});NodeContext is the shape you hand the application. Declaring it is what makes context.request and context.response resolve inside the resource.
Run the Drash App
npm startNote: npm start is defined in the package.json file.
Open It
Go to http://localhost:1447 . You should see something like the following:
Oh so easy (written at Wed Nov 01 2023 22:04:56 GMT-0400 (Eastern Daylight Time))Notes
Resources only recognize absolute URLs. Node’s request.url returns only a path, so it must be converted to a full URL first. For example:
/users/1 —> http://localhost:1447/users/1
Node uses the polyfill entry point, and its node:http server does not give you a Web Request — so the resource receives a context object that you build. The chain only requires a readable url and method; everything else on it is yours.
This example is in the repository at examples/node-ts.