Create a Web Server in Node.js with the HTTP Module on the AS400 (IBM i) server


Node.js is an open source project based on the Google Chrome JavaScript Engine. It provides a platform for server-side JavaScript applications running without browsers. Many developers are basing the architecture of their real-time applications on the Node.js framework because of its inherent event-driven architecture and non-blocking I/O API. Node.js can be run on the IBM i platform providing the reliability provided by this platform. In addition, extensions have been created to allow Node.js applications to access the IBM DB2 database and IBM i system resources.


Steps to try node.js on IBM i

1. Find an IBM i server to host your work

See if node is installed on your IBM i:
in IBM i QShell (command qsh)

> node -v       
  v18.17.1      

If not not installed. The Seiden Group provided excelent Guide to setting up node.js on IBM i

If you don't have node.js installed or a suitable server available, Pub400.com is a free and public Server running IBM i 7.5. Sign up here...
https://www.pub400.com/

If you need a 5250 client, try
IBM i Access Client Solutions (ACS)


2. You may want to download and install Visual Studio editor to write your code

https://code.visualstudio.com/download

You could do this edit EDTF on AS400 green screen or other text editor, but won't be as fun.


3. if you are using Visual Studio lookup and install Visual Studio "Code for IBM i"from within Visual Code



4. create a js file in IFS with your text editor.

/HOME/<your_username>/server.js

copy in code for a node.js server script

    const http = require('http');
    const server = http.createServer(function(req, res) {
          if (req.url === '/' ) {
          console.log(req);
          res.statusCode = 200;
          res.setHeader('Content-Type', 'text/plain');
          res.end('Hello, World!\n');
          }
    });
    
    server.listen(3030, function()  {
      console.log('Server listening on port 3030');
    });       

5. To run node.js programs you can use the built-in Windows Secure Shell utility (ssh). From the Windows Command Prompt type ssh. (Usually but not always port 443). Utility will prompt for password.

example:

ssh your_username@pub400.com -p 2222

Other ssh options include using puttty or the Chrome Secure Shell extension


6. to start server.js server, from ssh cmd line

node server.js

7. find your server's IP address

    > ping pub400.com
    
    Pinging PUB400.com [185.113.5.134] with 32 bytes of data:
    Reply from 185.113.5.134: bytes=32 time=112ms TTL=45
    Reply from 185.113.5.134: bytes=32 time=102ms TTL=45
    

8. Point your browser to your server program

type

http://185.113.5.134:3030/



Some additional references:

Liam Allan on Visual Code and IBM i extentions
Youtube: Getting started node web development on IBM i in PUB400 (yusy4code)
Youtube: Hello World to Node JS from IBM i (yusy4code)
Youtube: Creating REST Web service using Node JS in IBM i (yusy4code)
w3Schools Node js tutorial