Use Nodejs to build a simple web page and achieve public network access

Table of Contents

Preface

1. Install Node.js environment

2. Create a Node.js application

3. Install Cpolar intranet penetration to achieve public network access to Nodejs services

3.1 Register cpolar account

3.2 Download cpolar client

3.3 Create tunnel mapping local port

4. Fixed public network remote address


Foreword

Node.js is a web application framework built on Google Chrome’s JavaScript engine (V8 engine). Node.js comes with a runtime environment that can interpret and execute Javascript scripts (similar to the Java bytecode of the JVM). This runtime allows JavaScript code to be executed on any machine other than the browser. Since this runtime is on Node.js, JavaScript can now be on the server and executed.

Most of the basic modules of Node.js are written in JavaScript language. Before the emergence of Node.js, JavaScript was usually used as a client-side programming language, and programs written in JavaScript often ran on the user’s browser. The emergence of Node.js enables JavaScript to be used for server-side programming. Node.js contains a series of built-in modules that allow the program to be separated from Apache HTTP Server or IIS and run as an independent server. The following will introduce how to use Cpolar intranet penetration in a few simple steps to access the Windows node.js server under a remote public network. .

1. Install Node.js environment

Download node.js from the official website, we choose 64-bit one-click installation

https://nodejs.org/zh-cn/download/

image-20230302141011787

After installation, we open cmd, enter the command and the version number will appear normally, indicating that the installation is successful. The one-click installation version will configure the environment variables by default.

node -v

image-20230302150424377

2. Create Node.js application

Enter the command in the vscode console [note that you need to enter the corresponding file directory to execute the command]

Step 1:Import the required packages

Use the require directive to load the HTTP module.

var http = require("http")

Step 2: Create an HTTP server using the http.createServer method. Request and respond through parameter functions. Write a sample implementation that returns “Hello World”. The server listens on port 8081.

http.createServer(function (request, response) {
   // HTTP Status: 200: OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});
   // send the response body as "Hello World"
   response.end('Hello World\\
');
}).listen(8081);
// console will print the message
console.log('Server running at http://127.0.0.1:8081/');

Step 3: Create a js file helloworld.js in D:>Nodewang.

? File: helloworld.js

var http = require("http")
http.createServer(function (request, response) {
   response.writeHead(200, {'Content-Type': 'text/plain'});
   response.end('Hello World\\
');
}).listen(8081);
console.log('Server running at http://127.0.0.1:8081/');

Now run helloworld.js to see the results:

D:\\
odewang\hello1>node helloworld.js

Verify the output, the server application has been started!

Server running at http://127.0.0.1:8081/

image-20231109162434923

Browser access Node.js service

Open the browser and enter: http://127.0.0.1:8081/ in the address bar. In the browser, look at the following results.

image-20231109155239481

3. Install Cpolar intranet penetration to achieve public network access to Nodejs services

Here we use [cpolar] (cpolar – a secure intranet penetration tool) to perform intranet penetration of Nodejs. It supports http/https/tcp protocols, does not limit traffic, does not require public IP, and does not need to set up a router. It is simple to use. .

3.1 Register cpolar account

Enter the cpolar official website, click on free registration in the upper right corner, use your email to register a cpolar account for free and log in

cpolar official website address: https://www.cpolar.com/

20221117173301

3.2 Download cpolar client

After successful login, click to download cpolar locally and install it (you can install it by default all the way)

20221117173307

3.3 Create tunnel mapping local port

After cpolar is successfully installed, access the local 9200 port [http://localhost:9200] on the browser and log in using the cpolar account.

image-20231109160147636

Click Tunnel Management – Create Tunnel on the left dashboard to create an http tunnel pointing to the local port 3000.

  • Tunnel name: You can customize the name. Be careful not to duplicate the existing tunnel name.
  • Protocol: Select http
  • Local address: 8081
  • Domain name type: Choose a random domain name for free
  • Region: Select China vip

Click Create

image-20231109160513809

After the tunnel is successfully created, click the status on the left – Online tunnel list to view the generated public network access address. There are two access methods, one is http and https, both methods can be accessed!

image-20231109160613966

Open the browser and use the cpolar https public network address to access. You can see that the access is successful. In this way, the public network address accessed by the browser of a remote or any device is set, and the new address public network accesses the Nodejs service.

image-20231109160802071

4. Fixed public network remote address

Since the tunnel created using cpolar above uses a random public network address, it will change randomly within 24 hours, which is not conducive to long-term remote access. Therefore, we can configure a second-level subdomain name for it. This address is a fixed address and will not change randomly [ps: cpolar.cn has been filed]

Note that you need to upgrade the cpolar package to a basic package or above, and the bandwidth corresponding to each package is different. [cpolar.cn has been registered]

Log in to the cpolar official website backend, click Reserve on the left, select to reserve the second-level subdomain name, set a second-level subdomain name, click Reserve, and copy the reserved second-level subdomain name after the reservation is successful.

image-20231109161430804

After the reservation is successful, copy the reserved second-level subdomain name address

image-20231109161451496

Log in to the cpolar web UI management interface, click Tunnel Management – Tunnel List on the left dashboard, find the tunnel you want to configure, and click Edit on the right

image-20231109161245790

Modify the tunnel information and configure the successfully reserved second-level subdomain name into the tunnel.

  • Domain name type: Select a second-level subdomain name
  • Sub Domain: Fill in the successfully reserved second-level subdomain name

Click Update

image-20231109161530861

After the update is completed, open the online tunnel list. At this time, you can see that the public network address has changed and the address name has become a reserved and fixed second-level subdomain name.

image-20231109161703328

Then use the fixed http address to open the browser access

image-20231109161741676

The access is successful. Now the public network address is fixed and will not change randomly. Successfully penetrated the cpolar intranet to achieve remote access to nodejs services without requiring a public IP address or setting up a router.