Building Code Runner - Part #2 : Node.js Backend With Express Server and JDoodle API
Building a backend server application using Node.js, Express and JDoodle API to execute scripts and return the output as response.
Hello everyone ๐๐ฝ
Here is the third article in our series, CodeRunner. I am pretty much excited to get us started. Let's dive in ๐คฉ
This article will speak about 2 things:
JDoodle API
Using Express and Node.js
Introduction to the JDoodle API
JDoodle is an online compiler and editor just like our CodeRunner, built and maintained by @thenutpan. They also provide an API as a service to run scripts, by spending credits.
The pricing is pretty much reasonable and the free plan should help you get started for a personal project like this. You will be provided with 200 credits per day, which means 200 API calls per day. On signing up for the API, you will be provided with a client key and client secret which will be used for authenticating the API calls, so keep them handy.
The entire documentation for the API can be found here. We will be using the execute API for now.
Using Express and Node.js
Our application will be built on Node.js with express as our server. Do follow the steps in the previous part of the series before proceeding further.
- Create the required files and folders in your server directory to match the below directory structure.
- The
index.js
will be the entry point to our server. - The
controllers
folder will contain all the functions that shall be executed on the server. - The
models
folder consists of the Mongoose models (will be discussed in the next part). - The
routes
folder consists of the server routes which will point to a function based on the path.โโโ server โโโ controllers โ โโโ methods.js โโโ index.js โโโ models โ โโโ codes.js โโโ node_modules โโโ package.json โโโ routes โโโ paths.js
- The
- Express is a framework which enables web server capabilities to the Node.js. To get started, we will import the
express
library in ourindex.js
file and create an express app as follows.const express = require("express"); const app = express();
- To test our app, let's have a dummy route at
/
which returns a simple message.
Whenever a user reaches theapp.get("/", (req, res) => { return res.status(200).json({ message: "Up & Running " }); });
/
route of the app, they will be served with the message as response, showing that the server is active. - The app should be running and for that we need to provide a port, as follows.
Here, process.env.PORT refers to the PORT environment variable which will be setup by the hosting providers. In the case of it's absence, the application will run on port 8000.const port = process.env.PORT || 8000; app.listen(port, () => { console.log(`App is running on PORT : ${port}`); });
To enable the usage of environment variables, we will need to use thedotenv
library as follows and it should beline #1
of the program.require("dotenv").config();
To enable JSON capabilities to our application, we will need the
body-parser
library, so import it and use it in the app as follows.const bodyParser = require("body-parser"); app.use(bodyParser.json());
This sets up the entry point for the application.
Moving on to routes, the
paths.js
file shall contain the routes of the application and map it them to the controllers. It is easy to debug large application, by having the routes on a different folder.
The Express library provides the Router instance, that can be used to define the router paths. To begin, import express and the express Router as follows.const express = require("express"); const router = express.Router();
- Now, define the routes for the application based on the mind map and map them to the controller to execute and export them once ready. We need to import the controllers first and the code is as follows:
With this, we have our routes ready.const controller = require("../controllers/methods"); router.get("/getCode", controller.getCode); router.post("/runCode", controller.runCode); router.post("/saveCode", controller.saveCode); module.exports = router;