Creating Registration Form In Deno : MongoDB Setup, Saving Data
Tags: deno, mongodb, typescript
Published: 2020-06-20 17:08:02

<p>

Welcome again in the second series of our Building Registration form, in previous post we have added functionality of View Engine, Form Request now in this post we gonna setup our MongoDB and save our form data so without further ado, let’s get started …

First we need to create some file, so our file structure will look like.

</p>

<pre>

./
|-index.ejs
|-app.ts
|-static/
   |-style.css

</pre>

<p>

For our form styling, you can check our video

</p>

{% youtube awboDcBudYQ %}

<p>>

Inside our “index.ejs” our file gonna look like this

</p>

<pre>

&lt;head&gt;
&lt;link rel="stylesheet" href="/style.css">
&lt;/head>
&lt;body>
&lt;div class="container">
&lt;h1>Hello <%= data.msg %></h1>
 &lt;form method="POST" enctype="multipart/form-data" action="/post">
  &lt;div class="form-container">
 &lt;input type="email" class="form-input" name="email" required>
  &lt;label class="form-input-label">Email</label>
 &lt;span class="form-input-bar"></span>
&lt;/div>
&lt;div class="form-container">
  &lt;input type="password" class="form-input" name="password" required>
  &lt;label class="form-input-label">Password&lt;/label>
 &lt;span class="form-input-bar">&lt;/span>
&lt;/div>
&lt;div class='form-container'>
&lt;input type="submit" value="Submit">
&lt;/div>
&lt;/form>
&lt;/div>
&lt;/body>

</pre>

<p>

And our “style.css” will look like

</p>

<pre>

body{
font-family: Arial, Helvetica, sans-serif;
margin: 0;
padding: 0;
height: 100vh;
}
.container{
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
.form-container{
position: relative;
margin-bottom: 45px;
}
.form-input{
background: none;
background-color: white;
color: black;
font-size: 18px;
padding: 10px;
display: block;
border: none;
border-bottom: 1px solid #F8BBD0;
border-radius: 0px;
}
.form-input:focus{
outline: none;
border-bottom: none;
}
.form-input-label{
color: #F8BBD0;
font-size: 18px;
font-weight: normal;
position: absolute;
pointer-events: none;
left: 5px;
top: 1px;
transition: 0.2s ease all;
-webkit-transition: 0.2s ease all;
}
.form-input:focus ~ .form-input-label,.form-input:valid ~ .form-input-label{
top:-20px;
color: #E91E63;
}
.form-input-bar{
position: relative;
display: block;
width: 100%;
}
.form-input-bar:before,.form-input-bar:after{
content: '';
height: 2px;
width: 0;
bottom: 1px;
position: absolute;
background-color: #E91E63;
transition: 0.2s ease all;
-webkit-transition: 0.2s ease all;
}
.form-input-bar:before{
left: 50%;
}
.form-input-bar:after{
right: 50%;
}
.form-input:focus ~ .form-input-bar:before,
.form-input:focus ~ .form-input-bar:after{
width: 50%;
}

</pre>

![Image Styling looks something like this](https://dev-to-uploads.s3.amazonaws.com/i/fsbhk2b0efr4fsiykg8y.png)

<p>

Now let’s send our static so that web browser, first we need to import Send function from Oak module.

</p>

<pre>

import {Application, Router, send} from 'https://deno.land/x/oak/mod.ts';
.
.
.
// Sending Static file as Middleware
app.use(async (ctx,next) => {
 await send(ctx, ctx.request.url.pathname, {
  root: `${Deno.cwd()}/static`
 });
 next();
});

</pre>

<p>

Here we send our static file as middleware, using upper statement. Now let’s setup database, for our database we gonna use MongoDB for this we import our module as


</p>

<pre>

import {MongoClient} from ‘https://deno.land/x/mongo@v0.8.0/mod.ts';

</pre>

<p>

Let initiate our MongoClient class and setup our URI, for this example we are using local mongoDB data you can use mongoDB server from its website just copy the URL and paste inside clientURI.

</p>

<pre>

const client = new MongoClient();

client.connectWithUri("mongodb://localhost:27017");

</pre>

<p>

After initiating our mongoDB, Now create our collection and database as


</p>

<pre>

const db = client.database("registrationDB");
const dataCollection = db.collection('data');

</pre>

<p>

As our Boilerplate code for Database has finally finish let now save our data inside our POST method add following statement.

</p>

<pre>

.post('/post', async (ctx) => {
const form = JSON.stringify(await multiParser(ctx.request.serverRequest));
const parse = JSON.parse(form);
const dataSaved = dataCollection.insertOne(parse);
ctx.render('index.ejs',{data:{msg: parse.email}});
})


</pre>

<p>

Now open the browser and check our application is working or not.

In case you missed any part here is the full code.

app.ts

</p>

<pre>

// Importing our modules
import {Application, Router, send} from 'https://deno.land/x/oak/mod.ts';
import {viewEngine,
engineFactory,
adapterFactory} from 'https://deno.land/x/view_engine/mod.ts';
import {multiParser} from 'https://raw.githubusercontent.com/deligenius/multiparser/master/mod.ts';
import {MongoClient} from 'https://deno.land/x/mongo@v0.8.0/mod.ts';

const ejsEngine = engineFactory.getEjsEngine();
const oakAdapter = adapterFactory.getOakAdapter();
// Setting up mongoDB
const client = new MongoClient();
client.connectWithUri("mongodb://localhost:27017");
const db = client.database("registrationDB");
const dataCollection = db.collection('data');
// Initiate our app and router
const app = new Application();
const router = new Router();
// Send View Engine as middleware
app.use(viewEngine(oakAdapter, ejsEngine));
// Handling request inside router
router
.get('/', async (ctx) => {
ctx.render('index.ejs',{data: {msg: 'World'}});
})
.post('/post', async (ctx) => {
const form = JSON.stringify(await multiParser(ctx.request.serverRequest));
const parse = JSON.parse(form);
const dataSaved = dataCollection.insertOne(parse);
ctx.render('index.ejs',{data:{msg: parse.email}});
})
;
// Send our router as middleware
app.use(router.routes());
app.use(router.allowedMethods());
// Send our Static folder as middleware
app.use(async (ctx,next) => {
await send(ctx, ctx.request.url.pathname, {
root: `${Deno.cwd()}/static`
});
next();
});
// Listening to database
console.log('App is listening on Port 8000');
await app.listen({port: 8000})

</pre>

<p>

Now open you terminal and type command as “deno run — allow-net — allow-read — allow-write — allow-plugin — unstable app.ts” or your can use Denon.

Now type your email and password inside form element.

</p>

![Example Showing Email and Password Pass inside the server](https://dev-to-uploads.s3.amazonaws.com/i/f3otijy4w7srgzo3grb3.png)

<p>
And our database gonna look like this
</p>


![MongoDB data save inside the database](https://dev-to-uploads.s3.amazonaws.com/i/5o75d2xiyq9xzt0i9p6p.png)
<p>
So hope you like our post see in future posts until then 👋.
</p>

{% youtube RwFLpBJY2Es %}