A Node.js Backend without any server — Just Code using Webtask.io

Abhishek Rathore
Youstart Labs
Published in
5 min readJul 22, 2018
webtask.io is a simple platform for low scale backend service

Whenever, we talk about backend it is scary dark world — with no colors and things are terminal.

However, with platforms like firebase, it has become easier to make a backend. But, every backend comes with their SDK and style of coding/querying/APIs — which means there is a learning curve involved. It would be great if we can have an online IDE where we can run any of our NodeJS files and immediately get the backend working through APIs. We are trying the same thing on Webtask.io — it is an online IDE cum platform which will instantly deploy your code and give REST API over their domains. If you have a quick task of Node based backend and don’t want to dig into Linux cloud deployments — continue reading.

Initial Setup

After initial signup to WebTask.io you will get into their IDE — you can start via creating a new Web Task from a template. I chose ‘express with mongodb’. Check out the screens.

You will also have to find an online hosted MongoDB service — MLab is a perfect source for that. Sign up on MLab and get you hosted MongoDB URL.

If you don’t know about MLab, it is an online MongoDB which you can access via mongoldb URL like

mongodb://<dbuser>:<dbpassword>@ds147451.mlab.com:47451/youstart

Database user and password given in above URL will be made after making a new database on MLab. You can checkout MLab documentation for all this. You must choose sandbox free tier for having a free account.

A MLab Deployed Database

Word of Caution

Now comes the funny part — templates are outdated. Yes, I was using MongoDB 3 native driver (which you can check out in their NPM module section) but default connection code was given for MongoDB 2. This led to many errors while I was connecting to MLab Cloud MongoDB Database. This problem is solved in the next section.

Updating Environment Variables/ Secrets

You might be familiar with how node use environment variables (process.env) to keep secrets. However, WebTask has a section to keep secrets.

Secrets option is under the Wrench Icon Toolbar

You have to update in secrets — MONGO_URL. There can be many things which can go wrong with this URL config — Be Careful !! You will have to create a database and then a user for that database in MLab before jumping to this stage.

As of MongoDB native driver 3+ you can’t have MONGO_URL as :

mongodb://<dbuser>:<dbpassword>@ds147451.mlab.com:47451/youstart

Instead use the one given below ( checkout that we removed database name ‘youstart’ and added a section after ? )

mongodb://<dbuser>:<dbpassword>@ds147451.mlab.com:47451/?authSource=yourDB&w=1

`yourDB` should be replaced by your database name in MLab — else authentication will fail.

Also if you look at later URL you will see database name is removed — database name is now given in javascript code.

var db = client.db(‘youstart’); // Here youstart is the name of my database in MLab

I have edited the code of template to make it work, you can use the following code here — only one change needed is name of your database.

Complete Code (edited version of template)

‘use latest’;
import bodyParser from ‘body-parser’;
import express from ‘express’;
import Webtask from ‘webtask-tools’;
import { MongoClient } from ‘mongodb’;
import { ObjectID } from ‘mongodb’;
const collection = ‘my-collection’;
const server = express();
server.use(bodyParser.json());
server.get(‘/:_id’, (req, res, next) => {
const { MONGO_URL } = req.webtaskContext.secrets;
MongoClient.connect(MONGO_URL, (err, client) => {
const { _id } = req.params ;
if (err) return next(err);
var db = client.db(‘youstart’); //CHANGE THIS
db.collection(collection).findOne({ _id: new ObjectID(_id) }, (err, result) => {
if (err) return next(err);
res.status(200).send(result);
});
});
});
server.post(‘/’, (req, res, next) => {
const { MONGO_URL } = req.webtaskContext.secrets;
// Do data sanitation here.
const model = req.body;
console.log(MONGO_URL);
MongoClient.connect(MONGO_URL, (err, client) => {
if (err) return next(err);
var db = client.db(‘youstart’); //CHANGE THIS
db.collection(collection).insertOne(model, (err, result) => {
if (err) return next(err);
res.status(201).send(result);
});
});
});
module.exports = Webtask.fromExpress(server);

Use Runner to Test API

Click on the run button which will open an API test panel.

There are already 1 POST and 1 GET API endpoints implemented in the above code. You can first test the POST API to create some data on MLab database

POST /                  (with the body containing JSON data)

Check out result in MLab — you can check that some document will be created

Document created by POST API

Now you can test GET API

GET /:id  (where you can replace :id which Mongod Document ID) GET /5b532801161da900067f68a1

You will get the expected result in the Runner section.

Now we will check out our deployed backend server. Server URL is at bottom of IDE and you can start using it instantly.

 https://wt-06fb0e1f9b5c229c733b079d74404815-0.sandbox.auth0-extend.com/express-with-mongo-db/5b532801161da900067f68a1

Tada !! Server-less Backend is ready !!

--

--