Prerequisites
1. Choose a Runtime
You will need to choose a JavaScript runtime that:
- fits your use case(s); and
- provides APIs to create an HTTP server (e.g.,
http.createServer(...)in Node orDeno.serve(...)in Deno).
2. Install the Runtime
After choosing a runtime, you will need to install it on your machine (or the machine you plan to use). You can do this by following your chosen runtime’s documentation pages (typically under an “installation” page or similar).
3. Create an HTTP Server
Once your chosen runtime is installed and ready to be used, follow its documentation pages on creating an HTTP server. When you have an HTTP server set up, head back to this page and continue reading.
Unlike previous versions of Drash where Drash is a Deno HTTP server wrapper under the hood, Drash v3 is an agnostic codebase that can be used to create different types of functionality. This means Drash v3 does not provide APIs to create HTTP servers like what we did in v2. As a result, it can be used in any JavaScript runtime.
What does creating an HTTP server mean?
When we say “creating an HTTP server,” we mean writing some code that can listen for HTTP requests. As an example, if you chose Deno and wanted to create an HTTP server in it, that code could look like:
// See Deno's HTTP Server docs for more information:
// https://docs.deno.com/runtime/fundamentals/http_server/
Deno.serve({
hostname: "localhost",
port: 1447,
handler: async (req) => {
console.log("Method:", req.method);
const url = new URL(req.url);
console.log("Path:", url.pathname);
console.log("Query parameters:", url.searchParams);
console.log("Headers:", req.headers);
if (req.body) {
const body = await req.text();
console.log("Body:", body);
}
return new Response("Hello, World!");
},
});Taking this a step further, you could run the above code using:
deno run --allow-net app.tsWhen the code is runs (meaning the HTTP server is active), you will see:
Listening on http://[::1]:1447/This means it is listening for incoming HTTP requests. If you open http://localhost:1447 in your browser, the browser sends a request to that address. The server handles the request with return new Response("Hello, World!");, so you will see "Hello, World!" displayed on the page.