Creating a simple web server using the express
install express under the project file, This much said, quite simply, directly in the project directory
npm install express
create app.js file in the project directory, as follows
var express = require ( 'express) ; // introduced express module
var app = express (); // create an instance of the express
app.get ( '/', function (REQ, RES) {
res.send ( 'the Hello, myServer'); // server responds to the request
} );
app.listen (3000, function () {// listening 3000 port
console.log ( "Server running aT 3000 port");
});
run the project directory
node app.js
can see the results
open a browser to access: HTTP: // localhost: 3000/
2, add database, and the database output is connected to the console
herein with reference to a novice tutorial: tutorial novice
using a sample database novice tutorial, into the local database connection, as shown
on app.js the code changes
var express = require ( 'express' ); // introduced express module
var mysql = require ( 'mysql' ); // mysql module incorporated
var app = express (); // Create instance express
var connection = mysql.createConnection ({// Create instance mysql
Host: '127.0.0.1',
Port : '3306',
User: 'the root',
password: 'the root',
Database: 'myserver'
});
connection.connect ();
var = SQL 'the SELECT * the FROM Websites';
connection.query (SQL, function (ERR , result) {
IF (ERR) {
the console.log ( '[the SELECT ERROR]:', err.message);
}
the console.log (result); // database query result is returned to the
});
app.get ( '/', function (req,RES) {
res.send ( 'the Hello, myServer'); //// server responds to the request
});
Connection.end ();
app.listen (3000, function () {//// listening port 3000
the console.log ( 'Port Server running AT 3000');
});
then re-running node app.js when will find a database query returns data to the console in
to access the web page still does not change
3, returns the data to the database on the web page, the browser can be seen by accessing the database returns the results
again app.js modify, and then run the node app.js
var = express the require ( 'express'); // introduced express module
var mysql = require ( 'mysql' ); // mysql module incorporated
var app = express (); // Create instance express
var connection = mysql .createConnection ({// Create instance mysql
Host: '127.0.0.1',
Port: '3306',
User: 'the root',
password: 'the root',
Database: 'myserver'
});
connection.connect ();
var = SQL 'the SELECT * the FROM Websites';
var STR = "";
connection.query (SQL, function (ERR, Result) {
IF (ERR) {
the console.log (' [the SELECT ERROR]: ', err.message);
}
STR = the JSON.stringify (result);
data // database queries stored in the result, but the browser does not result directly read the result, and therefore needs to be parsed by the JSON
// Console .log (result); // database query returns the result to
the console.log (STR);
});
app.get ( '/', function (REQ, RES) {
res.send (STR); // server response to the request
});
connection.end ();
app.listen (3000, function () {//// listening port 3000
the console.log ( 'port Server running AT 3000');
});
You can see the output console, the database will become a parsing JSON object RowDataPacket
open the browser again
now, in fact, has been completed nodejs + express + mysql the easiest development, eliminating the tedious route, if you want page more beautiful, Ext or use CSS to write a page can not be repeated here.
----------------
Creating a simple web server using express
Guess you like
Origin www.cnblogs.com/kofsony/p/12522595.html
Ranking