FreeBSD Based Calculator App Using Node.js

In this tutorial, you will learn how to create a Calculator Application using modern server technology FreeBSD and Next.js as the main framework. This tutorial assumes that you have previous experience with Node.js.

This article was written using the FreeBSD 13.2 system. In this tutorial we will guide you in creating a calculator application that operates the basic functions of adding, subtracting, dividing, multiplying.

This Node.js calculator is very useful in your life, such as average, addition, multiplication, and other operations (such as modes) on a set of numbers. It is mainly created for standard deviation and other number operations.

Why did we choose Node.js as the main framework?, because Node.js is a server-side JavaScript runtime environment. The client-side JavaScript runtime environment is in the browser, but Node allows you to write server-side code using JavaScript.

The project we wrote contains a simple node.js application that exposes a REST API for performing arithmetic operations on integers, and provides a test suite with mocha and chai. The mocha-junit-reporters package is included to provide XML output that can be served in continuous integration tools such as Azure DevOps.


1. Creating the Calculator Script

The first step you have to do is initialize the Node.js project by creating a new folder for your project and open it in Putty or other terminal. Run the following command to initialize the Node.js calculator project.
root@ns7:~ # cd /usr/local/etc
root@ns7:/usr/local/etc # mkdir -p FreeBSD_Calculator_NodeJS
root@ns7:/usr/local/etc # cd FreeBSD_Calculator_NodeJS
root@ns7:/usr/local/etc/FreeBSD_Calculator_NodeJS # npm init
The above command is used to create a new folder called "zzz" and initialize your Node.js project with the "npm init" command. This command creates a package.json file with default settings.

Then, you create a JavaScript file in the Node.js calculator project folder, naming the JavaScript file, for example, "freebsd_calculator.js".

Open /root/.cache/pypoetry/index/reverseproxy/FreeBSD_Calculator_NodeJS/freebsd_calculator.js and Start Coding In calculator.js, you will write the code for your Node.js calculator. The following is the script that you have to write in the file.

var express = require('express'),
  app = express(),
  port = process.env.PORT || 3000;

app.use(express.static('public'));

var routes = require("./api/routes");
routes(app);

if (! module.parent) {
  app.listen(port);
}

module.exports = app

console.log("Server running on port " + port);

Then run the following command to include the Express dependencies into the contents of the "package.json" file.
root@ns7:/usr/local/etc/FreeBSD_Calculator_NodeJS # npm install --save-dev express
In the Node.js calculator project, create api, node_modules, test and public folders.
root@ns7:/usr/local/etc/FreeBSD_Calculator_NodeJS # mkdir -p api && mkdir -p node_modules && mkdir -p public && mkdir -p test
For more details, you just open our Github repository, namely unixwinbsd, and clone it to speed up the creation of the Node.js calculator project.
root@ns7:/usr/local/etc/FreeBSD_Calculator_NodeJS # git clone https://github.com/unixwinbsd/FreeBSD_Calculator_NodeJS.git
Run the following command to start installing.
root@ns7:/usr/local/etc/FreeBSD_Calculator_NodeJS # npm install
root@ns7:/usr/local/etc/FreeBSD_Calculator_NodeJS # npm run
Runs npm install : to install dependencies.
Runs npm run : to run Mocha and execute the unit tests.

The final step is to run the application, use the following command.
root@ns7:/usr/local/etc/FreeBSD_Calculator_NodeJS # node freebsd_calculator.js
Server running on port 3000
In the command above, you can see that the Node.js calculator application is running on IP 127.0.0.1 and port 3000.

Open Google Chrome and enter Node.js IP and port "http://127.0.0.1:3000/". You can see the results as in the image below.



In this article, you have created a simple calculator script using Node.js and FreeBSD as the main server. This project demonstrates how to handle user input, perform calculations, and provide output via the command line. This article will hopefully make it a valuable learning experience for those new to Node.js and JavaScript programming.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post