The first step
Download the corresponding node. js version to the node. JS website
Universal, fool-only installation, next step
The second step starts the node. JS First Experience
Create a new file named Hello.js
The contents are as follows
var http = require (' http '); Http.createserver (function (req, res) { Res.writehead, {' Content-type ': ' Text/plain ' }); Res.end (' Hello world node. js \ n '); }). Listen (1337, "127.0.0.1"); Console.log (' Server running at http://127.0.0.1:1337/');
Open the command line switch to the path of the script
Node Hello.js
Browser Access Address:
http://127.0.0.1:1337
If you see this page, congratulations on your entry into node. JS World
We can try to analyze this short piece of code
- The first line requests (require) node. js's own HTTP module and assigns it to the HTTP variable.
- Next we invoke the function provided by the HTTP module: Createserver. This function returns an object that has a method called Listen, which has a numeric parameter that specifies the port number that the HTTP server listens on.
Step three: Connect using node. js and MySQL
When node. js is installed by default, the module files are placed in the/usr/local/lib/node_modules directory;
node. js connection MySQL needs to use a node. JS component
1 $ cd/usr/local/lib 2Install
Create a test table
/*Navicat Premium Data Transfer source server:127.0.0.1 Source Server type:mysql source server Version: 50617 source host:127.0.0.1 source database:test target server type:mysql target server Version : 50617 File encoding:utf-8 date:08/20/2014 23:55:29 PM*/SETNAMES UTF8;SETForeign_key_checks= 0;-- ------------------------------Table structure for ' user '-- ----------------------------DROP TABLE IF EXISTS`User`;CREATE TABLE`User' (' userid 'int(3) not NULL, ' username 'varchar( -)DEFAULT NULL, ' Userage 'int(3)DEFAULT NULL, PRIMARY KEY(' userid ')) ENGINE=InnoDBDEFAULTCHARSET=latin1;-- ------------------------------Records of ' user '-- ----------------------------BEGIN;INSERT into`User`VALUES('1','Admin',' -'), ('2','Xainzhi','101');COMMIT;SETForeign_key_checks= 1;
To get data from a database connection, create a new file Sql.js
varMySQL = require (' MySQL ');varClinet =mysql.createconnection ({host:' localhost ', User:' Root ', Database:' Test ', Password:' Root ', Port:3306}); Clinet.connect (); Clinet.query (' SELECT * from user ',function(Err, rows, fields) {if(ERR)Throwerr; vardata = '; for(vari = 0; i < rows.length; i++) {Console.log (' <p> ' + ' User id: ' + rows[i].userid + ' </p> '); Console.log (' <p> ' + ' user name: ' + rows[i].username+ ' </p> '); Console.log (' <p> ' + ' user age: ' + rows[i].userage + ' </p> '); } }); Clinet.end ();
Open the command line switch to the path of the script
Node sql.js
The result of the operation is this
But control printing is not very good after all, the actual data to the user is viewed on the browser.
Fourth Step
Show the queried data to the browser
Here you need to install the node. JS Web Component
sudo Install -G Expresssudoinstall -D
In the browser display call JS
New File Demo.js
var express = require ('Express'); var MySQL= Require ('MySQL'); var app=Express (); App.use (function(req, res, next) {Console.log ('%s%s', Req.method, Req.url); Next ();}); var conn=mysql.createconnection ({host:'localhost', User:'Root', Database:'Test', Password:'Root', Port:3306}); Conn.connect (); App.get ('/',function(req, res) {Conn.query ('SELECT * from user',function(Err, rows, fields) {if(err) throw err; var data="'; for(var i =0; i < rows.length; i++) {Data+='<p>'+'User id:'+ Rows[i].userid +'</p>'; Data+='<p>'+'User name:'+ Rows[i].username +'</p>'; Data+='<p>'+'Age:'+ Rows[i].userage +'</p>'; Data+=''; } res.send (data); });}); App.listen ( the); Console.log ('Service url:http://127.0.0.1:3000');
Open the command line switch to the path of the script
Node demo.js
Going to this step will report an error,
Error:cannot find module ' Express '
It means that the express component cannot be found, but this component exists when viewed under/usr/local/lib/node_modules
You need to configure the environment variables to use this component
Export node_path= "/usr/local/lib/node_modules"
Now it is OK to see this picture is very kind.
These are your own step-by-step study, and if there's anything you don't understand, you can go see the next w3cschool and see the node. JS Tutorial