Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 10, 2021 05:34 pm GMT

Creating an API with Deno (import maps, deps.ts, etc.)

Getting Started

I created a template to sketch out many of the main files to get you up and running with deno. Make your own instance of the template by clicking "use template" and clone it to your workspace.

In the Template, there is a file called command.md that documents many of the mail commands and flags you need to work with deno

Importing Dependencies

import pogo from "https://deno.land/x/pogo/main.ts"

While we can do this we will use an import map to make our imports look cleaner.

  • inside the imports.json file include the following
{  "imports": {    "pogo": "https://deno.land/x/pogo/main.ts"  }}
  • instead of importing the dependencies all over our application, the conventional practice is to have a deps.ts files where you import all your dependencies and export them. Put the following in deps.ts.
//IMPORT THEN EXPORT DEPENDENCIESexport {directory, file, server, router} from "pogo"

Now we can import want we need from our dependencies from the deps.ts file.

  • For the purpose of making sure we consistently install dependencies we should generate a lock file by running the following command... deno cache --lock=deps_lock.json --import-map=imports.json --lock-write deps.ts. I would do this after installing new libraries.

Now if we wanted to upgrade the versions we use in our project we would just update the urls in our imports.json and it will update the whole project (rebuild your lock file after any new dep or upgrade).

Building the API

Head over to src/index.js and let's get started by creating the basics of our server.

// Import Server from depsimport { server } from "../deps.ts";// GET PORT FROM ENV VARIABLES ENABLED WITH THE --allow-env flag// ParseInt cause env variables come in as stringsconst PORT = parseInt(Deno.env.get("PORT"))// Create our Serverconst app = server({ port: PORT });app.router.get("/", (request, helper) => {  return { hello: "world" };});// START HTTP LISTENER ENABLED BY --alow-net flagapp.start();

run the following command to test the server
PORT=3000 deno run --allow-env --allow-net --import-map=imports.json src/index.js

Basic CRUD Example

Add the meals dummy data and routes

// Import Server from depsimport { server } from "../deps.ts";// GET PORT FROM ENV VARIABLES ENABLED WITH THE --allow-env flag// ParseInt cause env variables come in as stringsconst PORT = parseInt(Deno.env.get("PORT"))// Create our Serverconst app = server({ port: PORT });// DUMMY DATAconst meals = ["Breakfast", "Lunch", "Dinner"]app.router.get("/", (request, helper) => {  return { hello: "world" };});// MEAL CRUD ROUTES//indexapp.router.get("/meals", (request, helper) => {    return meals})//showapp.router.get("/meals/{index}", (request, helper) => {    return {meal: meals[parseInt(request.params.index)]}})//createapp.router.post("/meals", async (request, helper) => {    // parse the request body into a javascript object    const body = JSON.parse(new TextDecoder().decode(await Deno.readAll(request.body)));    meals.push(body.meal)    return meals})//updateapp.router.put("/meals/{index}", async (request, helper) => {    // parse the request body into a javascript object    const body = JSON.parse(new TextDecoder().decode(await Deno.readAll(request.body)));    meals[parseInt(request.params.index)] = body.meal    return meals})//showapp.router.delete("/meals/{index}", (request, helper) => {    const index = parseInt(request.params.index)    meals.splice(index, 1)    return meals})// START HTTP LISTENER ENABLED BY --alow-net flagapp.start();

Start the server and test out the routes using a tool like postman.

Conclusion

So you built an API, what now? You can deploy your deno app using docker to deploy a container to Heroku or Fly.io. Here is a video on how to do that.

You can add in a database like Mongo or Postgres. Once you get the hang of it, Deno offers some really interesting possibilities including compiling to executables.


Original Link: https://dev.to/alexmercedcoder/creating-an-api-with-deno-import-maps-deps-ts-etc-l25

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To