node web module (server and client)

Keywords: Web Server Database Nginx Javascript

node web module

web server

Web server refers to website server, which refers to a program residing on the Internet, the basic function of web browser, and provides information browsing service
The web supports the script language on the server side. The script language obtains data from the database and returns the results to the client browser

Basic architecture of web application

Client => Server => Business => Data
Client I.e. client, through http Protocol requests server
Server Server side web Server, receiving client request and sending response data to client
Business The business layer, through Web Server deals with application program, interaction of database, logical operation, calling external program
Data Data layer, storing data

Creating a Web server with Node

Create using http module

You need to use the substr() method, a method that returns the specified end from the specified location. The last parameter inherited from String can be saved https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/substr

// server.js
// Import module
var http = require('http'); // http Modular
var fs = require('fs');     // fs File module
var url = require('url');   // url Uniform resource locator module

// Create server
http.createServer((request, response) => {
    // Parse the request and save it in the variable
    var pathname = url.parse(request.url).pathname;

    // Filename of the output request
    console.log(pathname);

    // Read the file from the file system and return
    fs.readFile(pathname.substr(1), (err, data) => {    // Use substr Method to read the bytes of the file and return the file name to the callback function
        if (err){   // Handling errors
            console.log(err);   // Print out error
            // Return to one404
            response.writeHead(404, {'Content-Type': 'text/html; charset=utf-8'});
            response.write("Ah, nothing╮(╯_╰)╭");
            response.write('I guess what you want ' + data + ' ?');
            response.end();
        } else {
            // Return 200
            response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
            // Return to file content
            response.write(data.toString());    // String the contents of the read file and output
            response.end(); // Close connection,Send out data
        };
    });
}).listen(1937);
// index.html
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>This is a small Dome</title>
    </head>
    <body>
        <h1>hello word!</h1>
    </body>
</html>

Visit http://127.0.0.1:1937/index.html
hello word appears, complete!

PS C:\Users\mingm\Desktop\test> node Server.js
/index.html
/
/input.html
/
{ [Error: ENOENT: no such file or directory, open 'C:\Users\mingm\Desktop\test\input.html']
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'C:\\Users\\mingm\\Desktop\\test\\input.html' }

Improve a little

Visit / appear 404, indicating no home page is set, set home page

    // Add support for home page. Set the default home page as index.html
    if (pathname === '/') {
        pathname = pathname + 'index.html';
    }

The completed documents are as follows

// Import module
var http = require('http'); // http Modular
var fs = require('fs');     // fs File module
var url = require('url');   // url Uniform resource locator module

// Create server
http.createServer((request, response) => {
    // Parse the request and save it in the variable
    var pathname = url.parse(request.url).pathname;

    // Filename of the output request
    console.log(pathname);

    // Add support for home page,Set the default home page as index.html
    if (pathname === '/') {
        pathname = pathname + 'index.html';
    }

    // Read the file from the file system and return
    fs.readFile(pathname.substr(1), (err, data) => {    // Use substr Method to read the bytes of the file and return the file name to the callback function
        if (err){   // Handling errors
            console.log(err);   // Print out error
            // Return to one404
            response.writeHead(404, {'Content-Type': 'text/html; charset=utf-8'});
            response.write("Ah, nothing╮(╯_╰)╭");
            response.write('I guess what you want&nbsp;' + data + '&nbsp;?');
            response.end();
        } else {
            // Return 200
            response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
            // Return to file content
            response.write(data.toString());    // String the contents of the read file and output
            response.end(); // Close connection,Send out data
        };
    });
}).listen(1937);

Creating clients with node s

PS C:\Users\mingm\Desktop\test> node get.js
<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

PS C:\Users\mingm\Desktop\test>
var http = require('http');

// Requested options
var options = { // Create an object to save related data
    host:'www.iming.info',  // Host address
    port:'443', // Access port
    method:'GET',
    path:'/',   // Files accessed
};

// Callback function to process the response
var callback = (response) => {
    // Update data
    var body = '';
    response.on('data', (data) => {     // Binding event, data
        body += data;   
    });

    response.on('end', () => {  // Bind end event
        console.log(body);  
    });
};

// Start sending request
var req = http.request(options, callback);  // Send the request. options is the option to send the request. Callback is the callback function to process the request. Three events will be thrown, one for data, one for end and one for error. The end indicates that the request is completed and the connection is closed
req.end();  // Close connection

Because the small station uses nginx's https, uses a certificate, and needs to use the process of verifying the secret key, so it is denied access. 302 has no permission

Posted by fireant on Fri, 31 Jan 2020 01:59:15 -0800