What is the Express.js of Deno?

栏目: IT技术 · 发布时间: 5年前

内容简介:As first adopters tinker with Deno, many people are trying to figure out tooling that parallels what they’re used to in node. If you’re looking for a lightweight web application framework like express.js, you probably want to explore

What is the Express.js of Deno?

As first adopters tinker with Deno, many people are trying to figure out tooling that parallels what they’re used to in node. If you’re looking for a lightweight web application framework like express.js, you probably want to explore oak .

See oak on github

Note:Many of the examples in this post are taken from the github documentation!

What is Oak?

Oak is self-described as “A middleware framework for Deno’s http server, including a router middleware.”

To be completely transparent, oak is actually much more similar to koa for node than express.js, but express users should be very comfortable with oak.

Creating a Hello World App with Oak

To create a “Hello World!” app in deno with oak, we import Application from the oak module. You create a new instance of the app, add a handler for all requests that set the response body to "Hello World!" , and then listen on a port.

Make sure to pin down the version of oak in your import statement! Here it’s pinned down to version 4.0.0.

import { Application } from 'https://deno.land/x/oak@v4.0.0/mod.ts';

const app = new Application();

app.use(ctx => {
  ctx.response.body = 'Hello World!';
});

await app.listen({ port: 8000 });

Now if you browse to http://localhost:8000 , you should see our Hello world! message!

Adding Routes

Oak includes a router as well! Simply import the Router , create a new instance of it, specify your routes, and then make sure to add it as middleware to your app.

import { Application, Router } from 'https://deno.land/x/oak/mod.ts';

const books = new Map<string, any>();
books.set('1', {
  id: '1',
  title: 'The Hound of the Baskervilles',
  author: 'Conan Doyle, Author',
});

const router = new Router();
router
  .get('/', context => {
    context.response.body = 'Hello world!';
  })
  .get('/book', context => {
    context.response.body = Array.from(books.values());
  })
  .get('/book/:id', context => {
    if (context.params && context.params.id && books.has(context.params.id)) {
      context.response.body = books.get(context.params.id);
    }
  });

const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());

await app.listen({ port: 8000 });

Note the helpful router.allowedMethods() middleware, which will let clients know when a route is not allowed!

Onward!

This should hopefully get you underway! There is much more functionality available in oak, but now that you know the solution to persue and have had a basic primer, you should be able to go from here.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Java和Android开发学习指南(第二版)

Java和Android开发学习指南(第二版)

Budi Kurniawan / 李强 / 人民邮电出版社 / 2016-3 / 69.00元

本书是Java语言学习指南,特别针对使用Java进行Android应用程序开发展开了详细介绍。 全书共50章,分为两大部分。第1部分(第1章到第22章)主要介绍Java语言基础知识及其功能特性。第2部分(第23章到第50章)主要介绍如何有效地构建Android应用程序。 本书适合任何想要学习Java语言的读者阅读,特别适合想要成为Android应用程序开发人员的读者学习参考。一起来看看 《Java和Android开发学习指南(第二版)》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具