This is a documentation about how to use NodeJS to run a WebServer on FG160 and access it through a client device. This is a good reference to a CPE (Customer Premises Equipment) like a 5G Router or any other project that the module needs to serve a webpage.
This is an openLinux project, what means that you don't need to use an external MCU or MPU. All the software and processing is made by the FG160 internal MPU (Qualcomm SDX62 chipset), that has enough power to manage the modem and lot of more functionalities.
If this is your first contact with FG160 or OpenLinux I recommend you to check the NL668 OpenLinux Quickstart and new documentation that will be released here.
1.1 Understanding NodeJSNodeJS is a runtime that allows you to run JavaScript outisde a web browser. This means the FG160 module can execute almost any JavaScript program, like a WebPage or an API endpoint.
The usage of NodeJS give us a lot of possibilities, not just because the ease of programming, but because you can use a lot of software modules available on NPM and reuse some code of others applications.
It's important to mention that a NodeJS server usually uses a compiled application made on a development environment. Part of this compiled content will be interpreted on-demand by the WebBrowser when accessing (front-end) and the other part (back-end) will be interpreted by the system (SDX62 chipset in this case) using optimized code.
The interesting part of the compiled content, is that you not necessary need the NodeJS installed in your FG160 for some applications. For example, if you are serving a simple WebPage, the compiled output from a NodeJS build will be some HTML/CSS content that you can just move to the HTTPD directory (based on apache and already available on FG160). But if you need more functionalities, like run a more complex back-end that access GPIOs or system features, you may need the NodeJS installed.
Fortunately I will talk about both situations on this article and you will be able to set your FG160 to work as you wish.
If you want to understand more about NodeJS/JavaScript I recommend you to read this amazing article series from Lucas Santos about the details under the hood. The same content is available in English and Portuguese:
Node.js Por Baixo dos Panos [PT-BR]
2. Requirements- FG160 Module (preferably the devKit)
- Ethernet Adapter (preferably adapter for devkit CPB-WLAN-01-00)
- Ethernet Cable
- Android Debug Bridge (ADB) installed on your PC
- NodeJS installed on your pc
- FG160 CPE firmware (89115.1000.00.01.02.04_T01) installed*
*Obs.: This is a compiled FW to FG160 work with CPB-WLAN-01-00 that enables the Lan ETH 2.5G port on OpenLinux. You can achive the same results customizing your own FW, but the instructions for that will be provided in a future tutorial.
3. Installing NodeJS on your PCIf you already have the NodeJS installed in your PC, you can just skip to the next step.
If is not, I recommend you to install the NodeJS LTS (v16.x) following this link or the steps bellow:
3.1 Installing on LinuxRun on terminal:
# Using Ubuntu
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs
# Using Debian, as root
curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
apt-get install -y nodejs
3.2 Installing on WindowsDownload and run the NodeJS installer on this link:
4. Installing NodeJS on FG160Let's install the NodeJS runtime on FG160 to run our first hello world.
Access https://nodejs.org/en/download/, select LTS and download the Linux Binaries for ARMv7:
Turn on your board and check if is accessible via ADB:
adb devices
Obs.: If any devices appears, check if ADB interface is enabled on AT Serial port using AT+GTUSBMODE=17
or AT+GTUSBMODE=18
Enable root user on ADB and read-write mode on partitions:
adb root
adb shell mount -o rw,remount /`
Then go to the same folder of downloaded NodeJS binary and send the tar.gz file to /data/ partition using adb push
:
cd ~/Downloads/
adb push node-v16.15.1-linux-armv7l.tar.xz /data/
Access the openLinux using adb shell, then extract the tar file and rename it to node:
adb shell
cd /data
tar -xf node-v16.15.1-linux-armv7l.tar.xz
mv node-v16.15.1-linux-armv7l node
Create symbolic links to allow the running of node, npm and npx:
ln -s /data/node/bin/node /usr/bin/node
ln -s /data/node/bin/npm /usr/bin/npm
ln -s /data/node/bin/npx /usr/bin/npx
Check if node, npm and npx is working:
node -v
npm -v
npx -v
The most indicated process for NodeJS development is create a project in your own computer, then compile it and send the builded files to your device.
For development or debbuging reasons, it can be interesting run a interactive NodeJS or create an internal project in the FG160.
We will talk about the interactive and internal methods first, but you can skip directly to step 6 if you want to develop on the indicated process (external compiling).
5.1 Interactive NodeJS (optional)Lets create our first Hello World on NodeJS in a interative way. Is a really simple calculation and console print just to validate if everything is running.
Run the following commands:
node
a = 100;
b = 60;
family = "FG";
model = a+b;
console.log("Hello World! This is " + family + model);
Exit from NodeJS using Ctrl+C
or type .exit
.
If you want to compile/run a NodeJS project internal in FG160, you probably will need to have internet access to download* and install some NPM modules (this example should not require it).
*Obs.: You can avoid the download of NPM modules just pushing the modules folder from an external compiled project to FG160 and then running npm install.
Create a folder to your project, then create the.js file:
mkdir /data/projects
mkdir /data/projects/hello-world
cd /data/projects/hello-world
touch app.js
vi app.js
After open app.js with vi
, add the following content:
const http = require('http');
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
server.listen(port, () => {
console.log(`Server running on port ${port}`);
});
Save and quit using ESC
+ :wq
Then run your project:
node app.js
Now, connect your PC via Ethernet to LAN2 2.5G port on FG160 and access the gateway address (192.168.1.1:3000):
Here is the browser output:
We will create now an NodeJS project using React. This project will be modified and compiled on your computer and then we will push the builded files to the httpd folder, replacing the current http web page.
Open terminal in your computer workspace and run npx to create a react app example:
npx create-react-app hello-react
Enter in the project folder and edit the file /src/App.js
, replacing the line 10
to Hello world from FG160!
:
cd hello-react/
nano src/App.js
Build the project using npm
:
npm run build
NPM has compiled your project and the builded files for our WebPage has been storaged in the folder build/
, you can check it using ls -la build/
:
Remove the files from the current httpd directory /webs/web/
then push the new files using adb
:
adb shell rm -r /webs/web/*
cd build/
adb push * /webs/web/
Now, with your computer connected to FG160 via Ethernet on LAN 2.5G port, open your browser and access your gateway ip: http://192.168.1.1
Congratulations! You have finished your first NodeJS WebServer running on a 5G Module.
Comments
Please log in or sign up to comment.