Node.js provides a lot of useful modules and functions, they are very easy to use if you master how to use them. This article will show you two examples, the first example introduces how to use the node.js built-in http module to create an http web server. The second example will tell you how to install and use the node.js request function to get web page info from the internet.
1. Node.js Http Web Server Example.
- Because the Node.JS http module is a built-in module, so it has been installed automatically, we can just import and use it in JavaScript code directly.
1.1 Http Web Server Example Steps.
- Call require(“http”) method to import http related JavaScript library into your js code. And use a variable http_module to reference the http library.
- Create a javascript function that will process client requests and send response data back to the client when the http web server receives the request.
- Call http_module.createServer method to create an http web server. Pass the javascript function created in step2 as the input parameter.
- Call web_server.listen method to make the webserver listen on a special port.
- Print a message in the console to tell the user http web server has been started.
- web_server.js
// Include http module. Http module contains a lot of http related javascript functions. var http_module = require("http"); // Create a function to process client request and send response. function process_client_request(request, response) { // Below text will be sent back to client. var body_content = 'Hello this web server is implemented by Node.js'; // This is the body content length. var body_length = body_content.length; // Send header data to client. response.writeHead(200, { 'Content-Type':'text/plain', 'Content-Length': body_length}); // Send body text to client. response.end(body_content); } // Create web server. The web server will use above function to process client request. var web_server = http_module.createServer(process_client_request); // Web server listen on 8000 port. web_server.listen(8000); // Print server running message. console.log("Web server is running on port 8000, press Ctrl + C to exit. ");
- Save the above code in a js file web_server.js, run the below command in the shell terminal.
C:\WorkSpace\node-js-files>node web_server.js Web server is running on port 8000, press Ctrl + C to exit.
- Open browser, input http://localhost:8000 in the address input box. Click enter then you can see the below web page content.
Hello this web server is implemented by Node.js
2. Use Node.js Request Function To Get Web Page Content.
- Node.js request module is not a built-in module by default, so we need to install it first.
2.1 Install Node.js Request Module.
- Execute the command npm install request in the dos window. Then wait for npm to download and install the request module.
C:\> npm install request npm saveError ENOENT: no such file or directory, open 'C:\package.json’ npm saveError EPERM: operation not permitted, open ’C:\package—lock.json.1844882262 npm enoent ENOENT: no such file or directory, open 'C:\package.json’ npm !invalid#1 No description npm !invalid#1 No repository field. npm !invalid#1 No README data npm !invalid#1 No license field. + request@2.85.0 added 52 packages in 23.474s
- When you see the text + request@2.85.0 ( module version ) at the end of the installation process, it means the request module has been installed successfully.
2.2 Write And Execute Example Code.
- Use js editor to save the below code in a js file called crawl_web_page.js.
// Declare a variable that refrence node.js request module. var request_function = require("request"); // This is the web page url that we want to retrieve. var webpage_url = "http://www.bing.com"; // This function is used to process target url web page return result. function process_webpage(error, response, body) { // If no error occurred. if(!error) { /* response is an instance of class http.IncomingMessage. https://nodejs.org/api/http.html#http_class_http_incomingmessage */ // Print response status code. console.log('Server status code : ' + response.statusCode); // Print http version. console.log('Http version : ' + response.httpVersion); // Print request method. console.log('Request method : ' + response.method); // Get all header information in an array. var raw_headers = response.rawHeaders; // Get the header array length. var length = raw_headers.length; // Loop the array to get each header. for(var i=0;i<length;i++) { // Get header. var raw_header = raw_headers[i]; // Print the header. console.log(raw_header); } // Because body is too long, so we just print first 100 characters. body = body.substr(0, 100) + "..."; console.log(body); } } // Use the declared variable to request web page url, and use the process_webpage function to process returned web page data. request_function(webpage_url, process_webpage);
- Open dos window, input node crawl_web_page.js, then you can get below output in a dos window.
C:\WorkSpace\node-js-files>node crawl_web_page.js Server status code : 200 Http version : 1.1 Request method : null Cache-Control private, max-age=0 Content-Length 117841 Content-Type text/html; charset=utf-8 Vary Accept-Encoding P3P CP="NON UNI COM NAV STA LOC CURa DEVa PSAa PSDa OUR IND" Set-Cookie SRCHD=AF=NOFORM; domain=.bing.com; expires=Thu, 26-Mar-2020 12:39:10 GMT; path=/ Set-Cookie SRCHUID=V=2&GUID=E1A57CC8488A466E92F7D28A23455FA8&dmnchg=1; domain=.bing.com; expires=Thu, 26-Mar-2020 12:39:10 GMT; path=/ Set-Cookie SRCHUSR=DOB=20180326; domain=.bing.com; expires=Thu, 26-Mar-2020 12:39:10 GMT; path=/ Set-Cookie _SS=SID=1B7B853D6FAC622C0AA18E836E1B6342; domain=.bing.com; path=/ X-MSEdge-Ref Ref A: 3FDE964E27F644E780372156B8DCCD54 Ref B: BJ1EDGE0108 Ref C: 2018-03-26T12:39:10Z Set-Cookie _EDGE_S=F=1&SID=1B7B853D6FAC622C0AA18E836E1B6342; path=/; httponly; domain=bing.com Set-Cookie _EDGE_V=1; path=/; httponly; expires=Sat, 20-Apr-2019 12:39:10 GMT; domain=bing.com Set-Cookie MUID=2BD6D06FA530653F08F2DBD1A48764BD; path=/; expires=Sat, 20-Apr-2019 12:39:10 GMT; domain=bing.com Set-Cookie MUIDB=2BD6D06FA530653F08F2DBD1A48764BD; path=/; httponly; expires=Sat, 20-Apr-2019 12:39:10 GMT Date Mon, 26 Mar 2018 12:39:09 GMT Connection close <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtm...
I always enjoy guides that actually work. Nice write up! Thanks