Article directory
Preface
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.
Node.js Most of the basic modules 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 it in a few simple stepsCpolar intranet penetration Achieve access to windows node.js server under remote public network.
1. Install Node.js environment
Download node.js from the official website, we choose 64-bit one-click installation
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
2. Create a 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\n');
}).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\n');
}).listen(8081);
console.log('Server running at http://127.0.0.1:8081/');
Now run helloworld.js to see the results:
D:\Nodewang\hello1>node helloworld.js
Verify the output, the server application has been started!
Server running at http://127.0.0.1:8081/
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.
3. Install Cpolar intranet penetration to achieve public network access to Nodejs services
Here we use [cpolar](cpolar - secure intranet penetration tool) to carry out intranet penetration of Nodejs, support The http/https/tcp protocol does not limit traffic, does not require a public IP address, and does not require setting up a router. It is easy 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/
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)
3.3 Create tunnel mapping local port
After cpolar is successfully installed, access the local 9200 port on the browser [http://localhost:9200] and log in using the cpolar account .
Click Tunnel Management - Create Tunnel on the left dashboard to create an http tunnel pointing to the local port 3000.
- Tunnel name: The name can be customized. 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创建
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!
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.
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 one after the reservation is successful. Second-level subdomain name
After the reservation is successful, copy the reserved second-level subdomain name address
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
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更新
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.
Then use the fixed http address to open the browser access
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.