node. JS learning pathway (1)

Source: Internet
Author: User
Tags readfile

node. js belongs to the background language, background language and Php,java, etc.

Advantages: 1 Good performance node. js VS php 86 times times

2. js with front table convenient

3.node.js facilitates front-end learning

https://nodejs.org

node. js command

1. Switch Letter E:

2. Change the directory CD directory name

3. Execute the program node file name. js

JS can use node. js can be used


node. JS (with many modules to refer to the node. JS API)

node. JS ——— Server

http--protocol

Request input-requested information

Response response output-output information

Build a simple server:

Const HTTP = require (' http '); Request HTTP Module
Http.createserver (function (res,req) {//createserver (callback function) is used to create the server//Console.log ("Someone Came") Res.write ("A       BC ");          Res.end (); Listen//Port}). Listen (8080) file operation: Fs-flie Ssytem Synchronous vs Asynchronous synchronous-one asynchronous-multiple operations can be done at the same time, the previous operation is not finished, and the next operation//read-write file ReadFile (file name, function (Err,data) {}) WriteFile (file name, contents, function (Err,data) {})

Server Read-write case

Const HTTP = require (' http '); const FS = require (' FS ');
Http.createserver (function (req, res) {//req.url=> '/index.html '//Read = = './www/index.html '//'./www ' +req.url var file_name = './www ' + req.url; Fs.readfile (file_name, function (err, data) {if (err) {res.write (' 404 ')} else {res.write (data)} res.end ();});}). Listen (8080)   Request method: 1.get data in the URL 2.post data is no longer in the URL///////////////////////////////////////////////////////////// Req.url = "/aaa?user=blur&pass=1234"/aaauser=blurpass=1234////////////////////////////////////////////// Get parse path 1. Self-cutting 2.querystring modules     XXX=XXXX&XX=XX3.URL modules                  aaa?xxx=xx&xxx=12 urllib=require ("url"); Urllib.parse (url,true); pathname                                &N Bsp  querypost Parse Path  post data receive: Post data is much larger than get post very large __ Segment  const HTTP = require (' http '); const URLLIB = require (' URL '); Const querystring = require (' QuerystrinG '); Http.createserver (function (req, res) {//post-reqvar str = ';//Accept data var i = 0;req.on (' Data ', function (data) {Console. Log (' ${i++} Times received ') str + = data;}); Req.on (' End ', function () {var POST = querystring.parse (str); Console.log (POST)})}). Listen (8088)

Comprehensive case

Const HTTP = require (' http '); const FS = require (' FS '); const URLLIB = require (' URL '); Const querystring = require (' Querystri Ng '); Http.createserver (function (req, res) {//getconst obj = Urllib.parse (Req.url, True)
var url = obj.pathname; Const GET = obj.query;
POST var str = '; Req.on (' Data ', function (data) {str + = data;}); Req.on (' End ', function () {Const POST = querystring.parse (str);})/** * * */console.log (URL, GET, POST)//File request
var flie_name = './www ' + URL; Fs.readfile (Flie_name, function (err, data) {if (err) {res.write (' 404 ')} else {res.write (data)} res.end ();})}). Listen (6666); The above is the prelude to node's basic knowledge follow-up please continue to focus

node. JS learning pathway (1)

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.