{"id":651,"date":"2020-06-28T15:39:07","date_gmt":"2020-06-28T06:39:07","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2020\/06\/28\/try-deno"},"modified":"2020-06-28T15:39:07","modified_gmt":"2020-06-28T06:39:07","slug":"try-deno","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2020\/06\/28\/try-deno\/","title":{"rendered":"Deno\u3092\u89e6\u3063\u3066\u307f\u308b"},"content":{"rendered":"

\"\"<\/p>\n

\u6700\u8fd1\u30e2\u30f3\u30b9\u30bf\u30fc\u30a8\u30ca\u30b8\u30fc\u3092\u7bb1\u8cb7\u3044\u3057\u307e\u3057\u305f\u3002\u95a2\u53e3\u3067\u3059\u3002<\/p>\n

\u4eca\u56de\u306f\u4eca\u5e745\u6708\u306b\u6b63\u5f0f\u7248\u304c\u30ea\u30ea\u30fc\u30b9\u3055\u308c\u305f<\/a>Deno\u306b\u8efd\u304f\u89e6\u308c\u3066\u307f\u305f\u5185\u5bb9\u3092\u8a18\u8f09\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n

\u4e8b\u524d\u6e96\u5099<\/h2>\n

\u516c\u5f0f<\/a>\u3088\u308aDeno\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n

VSCode<\/a>\u3084JetBrainsIDE<\/a>\u3067\u306fDeno\u7528\u306e\u30d7\u30e9\u30b0\u30a4\u30f3\u304c\u65e2\u306b\u5b58\u5728\u3057\u307e\u3059\u306e\u3067\u3001\u3053\u3061\u3089\u3082\u9069\u5b9c\u5c0e\u5165\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n

\u7c21\u6613\u306aAPI\u3092\u4f5c\u6210\u3059\u308b<\/h2>\n

\u4eca\u56de\u306f\u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u3068\u3057\u3066oak<\/a>\u3092\u5229\u7528\u3057\u307e\u3059\u3002
\nApplication<\/code>\u3068Router<\/code>\u3092oak\u304b\u3089\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u3066\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u30b5\u30fc\u30d0\u30fc\u306e\u4f5c\u6210\u3068\u30eb\u30fc\u30c8\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n

app.ts<\/code><\/p>\n

import { Application } from 'https:\/\/deno.land\/x\/oak\/mod.ts'\nimport router from '.\/router.ts'\n\nconst env = Deno.env.toObject()\nconst HOST = env.HOST || '127.0.0.1'\nconst PORT = env.PORT || 7700\n\nconst app = new Application()\n\napp.use(router.routes())\napp.use(router.allowedMethods())\n\nconsole.log(`Listening on port ${PORT} ...`)\nawait app.listen(`${HOST}:${PORT}`)<\/code><\/pre>\n

route.ts<\/code><\/p>\n

import { Router }from 'https:\/\/deno.land\/x\/oak\/mod.ts'\nimport { getBooks, getBook, addBook, updateBook, deleteBook } from '.\/controller.ts'\n\nconst router = new Router()\nrouter.get('\/books', getBooks)\n    .get('\/books\/:isbn', getBook)\n\nexport default router<\/code><\/pre>\n

GET\u3092\u8a66\u3059<\/h3>\n

\u7c21\u5358\u306aGet\u30e1\u30bd\u30c3\u30c9\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n

controller.ts<\/code><\/p>\n

interface IBook {\n    isbn: string;\n    author: string;\n    title: string;\n}\n\nlet books: Array<IBook> = [{\n    isbn: "1",\n    author: "Robin Wieruch",\n    title: "The Road to React",\n},{\n    isbn: "2",\n    author: "Kyle Simpson",\n    title: "You Don't Know JS: Scope & Closures",\n},{\n     isbn: "3",\n     author: "Andreas A. Antonopoulos",\n     title: "Mastering Bitcoin",\n }]\n\nexport const getBooks = ({ response }: { response: any }) => {\n    response.body = books\n}<\/code><\/pre>\n

Deno\u3067\u66f8\u304b\u308c\u305f\u30a2\u30d7\u30ea\u3092\u8d77\u52d5\u3057\u307e\u3059<\/p>\n

$ deno run --allow-net --allow-env app.ts\nListening on port 7700 ...<\/code><\/pre>\n

http:\/\/localhost:7700\/books<\/code>\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001books<\/code>array\u304c\u8fd4\u3055\u308c\u307e\u3059\u3002
\n\"\"<\/p>\n

\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9<\/h2>\n

\u304a\u8a66\u3057\u3067\u5358\u7d14\u306a\u95a2\u6570\u3068\u305d\u308c\u306b\u5bfe\u5fdc\u3059\u308b\u5bfe\u5fdc\u3059\u308b\u30c6\u30b9\u30c8\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n

sample.ts<\/code><\/p>\n

export const add = (a: number, b: number) => {\n    return a + b;\n}<\/code><\/pre>\n

sample.test.ts<\/code><\/p>\n

import { add } from ".\/sample.ts";\nimport { assertEquals } from "https:\/\/deno.land\/std\/testing\/asserts.ts";\n\nDeno.test("it should add two numbers", () => {\n    assertEquals(add(1, 2), 3);\n});<\/code><\/pre>\n

deno test\u30b3\u30de\u30f3\u30c9\u3067\u30c6\u30b9\u30c8\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059<\/p>\n

$ deno test --allow-net\nrunning 1 tests\ntest it should add two numbers ... ok (8ms)\n\ntest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out<\/code><\/pre>\n

\u53c2\u8003<\/h2>\n

Simple REST API with Deno<\/a>
\n
Your First Deno Server in 60 Lines<\/a>
\n
How To Write Spec Tests In Deno<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

\u6700\u8fd1\u30e2\u30f3\u30b9\u30bf\u30fc\u30a8\u30ca\u30b8\u30fc\u3092\u7bb1\u8cb7\u3044\u3057\u307e\u3057\u305f\u3002\u95a2\u53e3\u3067\u3059\u3002 \u4eca\u56de\u306f\u4eca\u5e745\u6708\u306b\u6b63\u5f0f\u7248\u304c\u30ea\u30ea\u30fc\u30b9\u3055\u308c\u305fDeno\u306b\u8efd\u304f\u89e6\u308c\u3066\u307f\u305f\u5185\u5bb9\u3092\u8a18\u8f09\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002 \u4e8b\u524d\u6e96\u5099 \u516c\u5f0f\u3088\u308aDeno\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u304f\u3060\u3055\u3044\u3002 VSCode\u3084JetBrainsIDE\u3067\u306fDeno\u7528\u306e\u30d7\u30e9\u30b0\u30a4\u30f3\u304c\u65e2\u306b\u5b58\u5728\u3057\u307e\u3059\u306e\u3067\u3001\u3053\u3061\u3089\u3082\u9069\u5b9c\u5c0e\u5165\u3057\u3066\u304f\u3060\u3055\u3044\u3002 \u7c21\u6613\u306aAPI\u3092\u4f5c\u6210\u3059\u308b \u4eca\u56de\u306f\u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u3068\u3057\u3066oak\u3092\u5229\u7528\u3057\u307e\u3059\u3002 Applic […]<\/p>\n","protected":false},"author":10,"featured_media":684,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[264],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/651"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=651"}],"version-history":[{"count":0,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/651\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}