Node Web module (server side and client)

Source: Internet
Author: User
Tags readfile node server

Node Web module

Web server

Web server refers to a Web server, which is a program that resides on the Internet, the basic function of a Web browser, and provides information browsing service.
Web support server-side scripting language, which obtains data from a database through a scripting language and returns the results to the client browser

Basic Architecture for Web applications
Client => Server => Business => DataClient 即客户端,通过http协议向服务器发起请求Server 服务器端,指web服务器,接收客户端请求,并向客户端发送响应的数据Business 即业务层,通过Web服务器处理应用程序,数据库的交互,逻辑运算,调用外部程序Data 数据层,储存数据
Create a Web server using node

Using the HTTP module to create

You need to use the substr () method, a method that returns a specified end from a specified location, and inherits from the last parameter of the string to save https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/ Reference/global_objects/string/substr

server.js//introduces module var http = require (' http ');     HTTP Module var fs = require (' FS ');   FS file module var url = require (' URL '); URL Uniform Resource Locator module//create server Http.createserver (request, response) = {//parse request, save into variable var pathname = Url.parse (reques    T.url). Pathname;    The file name of the output request Console.log (pathname); Reads the file from the file system, returns the Fs.readfile (Pathname.substr (1), (err, data) + {//bytes that are read with the Substr method, returns the file name to the callback function if (ERR)   {//Error handling Console.log (ERR);            Print out error//return a 404 Response.writehead (404, {' Content-type ': ' Text/html; Charset=utf-8 '});            Response.Write ("Ah, nothing ╮ (╯_╰) ╭");            Response.Write (' I guess you want   ' + data + ';? ');        Response.End ();            } else {//returns Response.writehead ($, {' Content-type ': ' Text/html; Charset=utf-8 '});    Returns the contents of the File Response.Write (data.tostring ()); The contents of the read file are serialized and output response.end ();    Close connection, send data}; });}). Listen (1937); 
// index.html<!doctype html>

Visit http://127.0.0.1:1937/index.html
appear Hello Word finish!

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

Access/appearance 404, description Not set home page, set homepage

    // 增加对首页的支持,设置默认首页为index.html    if (pathname === ‘/‘) {        pathname = pathname + ‘index.html‘;    }

The completed file is as follows

Introduce module var http = require (' http ');     HTTP Module var fs = require (' FS ');   FS file module var url = require (' URL '); URL Uniform Resource Locator module//create server Http.createserver (request, response) = {//parse request, save into variable var pathname = Url.parse (reques    T.url). Pathname;    The file name of the output request Console.log (pathname);    Added support for home page, set default home for index.html if (pathname = = =/') {pathname = pathname + ' index.html ';        }//Read file from File system, return Fs.readfile (PATHNAME.SUBSTR (1), (err, data) + = {//Use substr method to read bytes of file, return file name to callback function   if (err) {//errors are handled console.log (ERR);            Print out error//return a 404 Response.writehead (404, {' Content-type ': ' Text/html; Charset=utf-8 '});            Response.Write ("Ah, nothing ╮ (╯_╰) ╭");            Response.Write (' I guess you want &nbsp; ' + data + ';? ');        Response.End ();            } else {//returns Response.writehead ($, {' Content-type ': ' Text/html; Charset=utf-8 '}); Returns the file content Response.Write (data.ToString ()); The contents of the read file are serialized and output response.end ();    Close connection, send data}; });}). Listen (1937);
Creating a client with node
PS C:\Users\mingm\Desktop\test> node get.js
var http = require(‘http‘);// 请求的选项var options = { // 创建一个对象保存相关数据 host:‘www.iming.info‘, // 主机地址 port:‘443‘, // 访问端口 method:‘GET‘, path:‘/‘, // 访问的文件};// 处理响应的回调函数var callback = (response) => { // 更新数据 var body = ‘‘; response.on(‘data‘, (data) => { // 绑定事件,data body += data; }); response.on(‘end‘, () => { // 绑定end事件 console.log(body); });};// 开始发送请求var req = http.request(options, callback); // 发送请求,options为发送请求的选项,callback为处理请求的回调函数,将会有抛出三个事件一个data一个end,一个error,必须有end表示请求完毕,关闭连接req.end(); // 关闭连接

Because the station is using Nginx HTTPS, using the certificate, need to use the process of verifying the secret key, so access denied, 302 no permissions

Node Web module (server side and client)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.