Step-by-step Nodejs+express+mysql Simple instance development
1. Create a simple Web server with Express
Project files installed under Express, this is not much to say, very simple to run directly in the project directory
NPM Install Express
Create the App.js file under the project directory with the following code
var express = require (' Express); Introducing the Express module
var app = Express (); Create an instance of Express
App.get ('/', function (req, res) {
Res.send (' Hello,myserver '); Server Response Request
});
App.listen (3000,function () {//listening on 3000 ports
Console.log ("Server running at Port");
});
Run under project directory
Node App.js
can see the results
Open Browser access: http://localhost:3000/
2. Add database, connect to database and output in console
Here's a beginner's tutorial: Beginner's Guide
Using the sample database in the Rookie tutorial, import into the local database connection,
To modify the code in App.js
var express = require (' Express '); Introducing the Express module
var mysql = require (' mysql '); Introducing the MySQL module
var app = Express (); Create an instance of Express
var connection = Mysql.createconnection ({//create MySQL instance
Host: ' 127.0.0.1 ',
Port: ' 3306 ',
User: ' Root ',
Password: ' Root ',
Database: ' MyServer '
});
Connection.connect ();
var sql = ' SELECT * from websites ';
Connection.query (SQL, function (err,result) {
if (err) {
Console.log (' [SELECT ERROR]: ', err.message);
}
Console.log (result); Database query results are returned to result
});
App.get ('/', function (req,res) {
Res.send (' Hello,myserver '); Server Response Request
});
Connection.end ();
App.listen (3000,function () {////Listening 3000 ports
Console.log (' Server running at Port ');
});
When you run node App.js again, you will find that the database query data is returned to the console
Access to Web pages is still unchanged
3, return data from the database to a Web page, access the browser to see the database return results
and then modify the App.js, and then run node App.js
var express = require (' Express '); Introducing Express Module
var mysql = require (' mysql ');//Introducing MySQL module
var app = Express ();//Create an instance of express
var connection = Mysql.createconnection ({//create MySQL instance
Host: ' 127.0.0.1 ',
Port: ' 3306 ',
User: ' Root ',
Password: ' Root ',
Database: ' MyServer '
});
Connection.connect ();
var sql = ' SELECT * from websites ';
var str = "";
Connection.query (SQL, function (err,result) {
if (err) {
Console.log (' [SELECT ERROR]: ', err.message);
}
str = json.stringify (result);
Database query data is saved in result, but the browser does not directly read results in result, so it needs to be parsed with JSON
Console.log (result); Database query results are returned to result
Console.log (str);
});
App.get ('/', function (req,res) {
Res.send (str); Server Response Request
});
Connection.end ();
App.listen (3000,function () {////Listening 3000 ports
Console.log (' Server running at Port ');
});
You can see the output information of the console and parse the Rowdatapacket in the database into a JSON object
Open your browser again
At this point, actually has completed the nodejs+express+mysql of the simplest development, eliminating the cumbersome route, if you want to make the page more beautiful, use ext or CSS to write the page can be, here no longer repeat.
A simple example of mastering Nodejs in one step