Simple Deno API with testing part

Deno is a simple, modern, and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust. You can look more about Deno on his official website
Steps
- Install Deno.
- Create API
- Run the API with permissions.
- Test our API
1. Installation
Deno works on macOS, Linux, and Windows. Deno is a single binary executable. It has no external dependencies.
Download and install
deno_install provides convenience scripts to download and install the binary.
Using Shell (macOS and Linux):
$curl -fsSL https://deno.land/x/install/install.sh | sh
Using PowerShell (Windows):
$iwr https://deno.land/x/install/install.ps1 -useb | iex
Deno Update
To update a previously installed version of Deno, you can run:
$deno upgrade
2. CreateAPI
You have to create the project structure first of all it will be simple and easy just navigate to your Desktop and run this command :
$mkdir denoAPi
Open your text editor , and create a file named index.ts or server.ts
we are going to use TYPESCRIPT
import { Application, Router } from 'https://deno.land/x/oak@v4.0.0/mod.ts'
// here we have the default values for the host parameters
const PORT = 8080
const HOST = 'localhost'
// APP and Routes
const app = new Application()
const router = new Router()
// API with only a get method
router.get('/api', (context) => {
context.response.body = 'Welcome to your first DenoAPI ! Keep it!'
})
// We make the app use the routes
app.use(router.routes())
app.use(router.allowedMethods())
// Run configuration for the app
const HOST_PORT = `${HOST}:${PORT}`
console.log(`Listen on ${HOST_PORT}`)
app.listen(HOST_PORT)
Add this to the index.ts
file.
3. Run the API with permissions.
Deno is secure by default. Therefore, unless you specifically enable it, a deno module has no file, network, or environment access for example. Access to security sensitive areas or functions requires the use of permissions to be granted to a deno process on the command line.
For the following example, xxx.ts
has been granted read-only access to the file system. It cannot write to it, or perform any other security sensitive functions.
$deno run --allow-read xxx.ts
To run the Our API we need to set the --allow-net
flag to use the network protocols in our app. Now, run this script in your path where the index.ts
is.
$ deno run --allow-net ./index.ts
4. Test our API
Now we can go to our browser and test the API at http://localhost:8080/api
.
It was a very simple example of Deno you have to search more and discover all the features off this new technology.
Enjoy !