📅  最后修改于: 2023-12-03 14:40:42.367000             🧑  作者: Mango
Deno is a JavaScript runtime created by a prominent member of the Node.js team, Ryan Dahl. It is designed with modern web applications in mind and aims to provide a more secure and reliable alternative to Node.js.
One of the key features of Deno is its built-in module system, which allows developers to import modules directly from URLs. This means that Deno does not require a package manager or a centralized registry, making it a more lightweight and flexible solution.
Deno Init is a command-line interface tool that allows you to easily create a new Deno project. To use Deno Init, simply open your terminal and type:
deno init myproject
This will create a new folder called "myproject" in your current directory, with the following structure:
myproject/
├── mod.ts
├── README.md
├── .gitignore
The "mod.ts" file is the entry point for your application, and you can start coding your application by editing this file. The "README.md" file is a template for a README file, which you can fill out with information about your project. The ".gitignore" file is a template for a Git ignore file, which will exclude certain files and folders from being tracked by Git.
Let's create a simple HTTP server with Deno Init. First, we need to install the "http" module:
import { serve } from "https://deno.land/std/http/server.ts";
const server = serve({ port: 8000 });
console.log("Server running on port 8000");
for await (const req of server) {
console.log("Request received:", req.url);
req.respond({ body: "Hello, Deno!" });
}
The "serve" function is a part of the Deno Standard Library and creates a new HTTP server on port 8000. When a request is received, it logs the requested URL to the console and responds with a simple "Hello, Deno!" message.
To run this server, save the file as "mod.ts" and run:
deno run --allow-net mod.ts
This will start the server on port 8000, and you can open http://localhost:8000 in your browser to see the "Hello, Deno!" message.
Deno Init is a simple but powerful tool for starting new Deno projects. With its built-in module system and lightweight approach, Deno is a promising alternative to Node.js for modern web development. Give it a try and see what you can build!