1.nodeJs Overview
There seems to be a JS in the back, that is the same as in our brains JS ~ ~ ~ Hahaha, you guess
What kinds of objects do we know about JavaScript?
Built-in objects, local objects, host objects
Our JS is running on the client side
node. JS is the language that runs on the server, so who does it rival? PHP, JSP, ASP, etc.
Nodejs What object Ah, in addition to some of the original ECMA object, there are 1000+ objects, haha haha, 6 not 6
For us, a language all-around, and can increase our competitiveness, ha ha, control of their own emotions
Node is a V8 engine-based JS runtime environment written in C + + and provides many ECMAScript-based extension objects.
So what's the difference between Nodejs and other back-end programming languages?
Nodejs is a service-side language developed based on the ECMAScript language that can write standalone server-side applications or provide Web content to clients without the need to connect to the file system with any Web servers (Apache) and to manipulate the database
Nodejs for high concurrency, I/O intensive, scalable network applications, data write-to-read applications are better
Nodejs is not suitable for CPU-intensive applications, and is not suitable for a variety of calculations
How about the performance of Nodejs, and PHP to do comparison, if there is 3000 concurrent connection, sustained 30 seconds of pressure
Output "Hello World" in response,
PHP responds to 3,624 requests per second
Nodejs response to 7,677 requests per second
So what if we operate MySQL?
PHP responds with a request of 1293 per second
Nodejs Response Requests per second 3,000
It's also said that Nodejs is much better than PHP when it comes to Web applications.
So is it that node's performance is always good?
The value of Nodejs is, for us, the best is the opportunity and platform that node offers us.
Two modes of operation in 2.nodejs
1. Interactive mode: REPL mode read evaluate print loop
Also directly in the CMD input node can be in the cmd to write JS code, and Google Browser console, and, with the printing function, this is to identify our operation, and print out, suitable for testing, but note yo, there is no BOM and Dom object Oh, for example, try Alert Bai
Enter a row, execute one line
2. Script mode: Put the code you want to execute in a file and run it
can have JS suffix, can also be any suffix is not
Node Xx.js
This is the model for developing applications.
2.nodeJs Common Module-----QueryString module
What is a module and think about the box of square bricks?
Because Nodejs have 1000+ object, too messy, it is the global object, it is more difficult to deal with, node put a bunch of objects, put into the classification of the module
The HTTP protocol specifies that the request message can be appended to the URL by appending a querystying, which is used to submit the request data to the server
Https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=node.js&oq=node%E6 %96%87%e6%a1%a3&rsv_pq=f3436c790004285f&rsv_t= 7782aw2lxngw1ezviwjtvwsnbma6tbjeh3kuyasqs6ddkvsleczqb529vac&rqlang=cn&rsv_enter=1&inputt=639& Rsv_sug3=38&rsv_sug1=28&rsv_sug7=100&bs=node%e6%96%87%e6%a1%a3
? The back of this pile is querystring.
Then node provides a querystring module for parsing the querystring of key-value pairs
Querystring.stringify (obj[, sep][, eq][, Options])
Serializes an object into a query string.
Querystring.parse (str[, sep][, eq][, Options])
Deserializes a query string into an object
var qs=require ("QueryString");
var obj = qs.parse (' Foo=bar&baz=qux&baz=quux&corge ');
Console.log (obj);
var str=qs.stringify (obj);
Console.log (str)
5.nodejs Common Module-----URL module
This module contains tools for parsing and parsing URLs. Call require (' URL ') to access the module.
Operation to see the document OH ~ ~ ~
6.nodejs Common Module-----filesystem module
Used to manipulate the file system---read-write directories and read-write files
There are a lot of functions in this module, let's learn two.
1. read file asynchronous Read Fs.readfile
var fs = require (' FS ');
Fs.readfile ('./file/a.html ', function (err,data) {
Console.log (data.tostring ())
})
Console.log (1)
Output 1 First, then output content
Synchronous execution
var buff=fs.readfilesync ('./file/a.html ');
Console.log (buff.tostring ())
Console.log (1)
Outputs the content, in the output 1
Note that this is a bunch of "buff 05 ..." Things that are not converted to a string. This is a way to display the file after it is put into the buffer, it needs to be converted
2. Writing files
var fs = require (' FS ');
var html = ' System log: ' +new Date ();
Fs.writefilesync ("./file/system.log", HTML);
Which file to write, if not this file, will be created, but must have this directory
So this is synchronous writing, and of course it's asynchronous.
Fs.writefile (' Message.txt ', ' Hello Node ', function (err) {
if (err) throw err;
Console.log (' it\ ' s saved! ');
});
But notice, will cover all, so bad, append or good point, to see if there is
Fs.appendfilesync (filename, data[, options])
And of course it's asynchronous.
Fs.appendfile (' message.txt ', ' data to append ', function (err) {
if (err) throw err;
Console.log (' The ' data to append ' is appended to file! ');
});
Do a task, make a copy, copy a file.
var fs=require ("FS");
var html=fs.readfilesync ("./file/a.html");
Fs.writefilesync ("./file/aa.html", HTML)
7.nodejs Common Module-----HTTP Module
HTTP protocol content:
1. Request message
2. Response messages
The Nodejs HTTP module can be used to accept the client's request message, read the request data in it, and return the response message to the client--web server
Which means that this module is the module that implements the Web server
How to write it?
var http = require (' http ');
var url=require ("url");
and create a Web server
var server = Http.createserver ();
Allow the Web server to handle client connection requests, callback two parameters are request message and response message
Server.on ("Request", function (request,response) {
Console.log (arguments)
})
To have the Web server listen on the specified port
Server.listen (1234,function () {
Console.log ("Start monitoring Yo")
})
The request has information such as the requested header, the request method, the requested URL, etc.
Practice, get the data you've passed.
Console.log (Url.parse (request.url,true). Query)
Here is the server received the request, then how to do a return it, can not always turn it
To set the response status code and the response header, you must have this response header
Response.writehead (200,{' content-type ': ' Text/html;charset=utf-8 ');
Output response Content
Response.Write ("Response.Write (' <p>welcome~~~</p> ');
End the output of the response body
Response.End ();
But now no matter how the address bar behind write we can only return to this stuff, then why???
Do a practice yo. Let us enter a different thing to access the page is not the same, if not, skip to page 404
var sourcename= './file/' +request.url.substr (1);
var html;
Fs.readfile (Sourcename,function (err,data) {
if (err) {
Html=fs.readfilesync ("./file/404.html");
}else{
Html=data.tostring ();
}
Response.writehead (200,{' content-type ': ' Text/html;charset=utf-8 ');
Response.Write (HTML);
Response.End ();
});
8.nodejs Common third-party modules----MySQL module
Nodejs officially provides twenty or thirty modules, but not enough, such as the lack of access to the MySQL database module, need to download third-party URLs, such as the world's largest Nodejs third-party module distribution Center:
https://npmjs.org
After you put it, you can try to connect to the database and manipulate the data, execute the SQL statements, play
var mysql=require ("MySQL");
var conn=mysql.createconnection ({
Host: "127.0.0.1",
User: "Root",
Password: "",
Database: "Test"
})
var sql= "INSERT into ' test '. ' User ' (' id ', ' name ', ' Password ') VALUES (NULL, ' Tom ', ' AA ');"
Conn.query (Sql,function (err,result) {
if (err) {
Console.log (ERR);
}else{
Number of rows affected
Console.log (Result.affectedrows)
ID of the Insert
Console.log (Result.insertid)
}
})
Conn.end ();
Practice SELECT statement
var sql= "select * from ' Test '. ' User '"
Conn.query (Sql,function (err,result) {
if (err) {
Console.log (ERR);
}else{
Console.log (Result)
}
})
node. js