Starting Github/blog welcome comments to the Stars
Installation
First assume that you have node. js installed, then create a directory for your app, then install the Express-generator app skeleton
$ mkdir node-demo$ npm install express-generator -g //mac需要加sudo
Express-h
$ express -h 用法: express [options] [dir] 选项: -h, --help 输出使用信息 -V, --version 输出版本号 -e, --ejs 添加ejs引擎支持(默认为jade) --hbs 添加handlebars引擎支持 -H, --hogan 添加hogan.js引擎支持 -c, --css <engine> 添加stylesheet <engine> 支持 (less|stylus|compass|sass) (默认为CSS) --git 添加.gitignore -f, --force 非空目录上的 force
Express-generator Creating an application skeleton
$ express node-demo
After you execute this command, you have the following code
Warning:the default view engine is not being jade in the future Releaseswarning:use '--view=jade ' or '--help ' for additional options//WARNING: The default view engine will not be a jade//warning in future releases: use "-view = JADE" or "help" option//Here we regenerate the skeleton by default to Ejs template using the following command//express--view=ejs blog//- -view= the name of the template engine, there are many kinds of//blog for the project folder name $ Express--view=ejs Node-demo Use the Ejs template engine//The following information appears, installation instructions step-by-step create:node-demo/cre ate:node-demo/public/create:node-demo/public/javascripts/create:node-demo/public/images/create:node-demo/ Public/stylesheets/create:node-demo/public/stylesheets/style.css Create:node-demo/routes/create:node-demo/ro Utes/index.js create:node-demo/routes/users.js Create:node-demo/views/create:node-demo/views/error.jade CRE Ate:node-demo/views/index.jade Create:node-demo/views/layout.jade create:node-demo/app.js create:node-demo/p Ackage.json create:node-demo/bin/create:node-demo/bin/www Change Directory: $ cd Node-demo install depend Encies: $ NPM Install run The app: $ debug=node-demo:* npm Start
Run Express
$ npm start //浏览器输入 http://localhost:3000///即可看到//Express//Welcome to Express
Project file Analysis
After the project is successfully created, four folders and two files are generated,
- App.js
- This is its initial form, and this module will continue to export to the Bin folder under the WWW file use
- Configuration information File Packetage.json
- Bin is the startup file for the project, which configures how the project is started, the default NPM start
- www file: Here is the basic configuration of the HTTP server
- Public is the static file of the project, place JS CSS img and other files
- Routes is the routing information file for the project, which controls the address routing
- Views are view files, place template files Ejs or jade etc. (in fact, the equivalent of HTML file ~)
- An MVC framework pattern like Express is a basic component of a Web project.
Evolutionary projects-Connecting databases
$ npm install mysql --save
In the root SQL folder, create a new sqlconfig.js with the following content:
// 引入mysqlvar mysql = require(‘mysql‘);var pool = mysql.createPool({ host: "localhost", //这是数据库的地址 port: "2000", user: "root", //需要用户的名字 password: "12345", //用户密码 ,如果你没有密码,直接双引号就是 database: "huang" //数据库名字});/** * @param {*} sql sql语句 * @param {*} callback 回调函数 */function query(sql, callback) { pool.getConnection(function (err, connection) { connection.query(sql, function (err, rows) { callback(err, rows); connection.release(); //释放链接 }); });}exports.query = query;
Routes folder below the new admin_user.js corresponding Admin_user table module contents as follows:
var express = require(‘express‘);var router = express.Router();//引入数据库包var sql = require("../sql/sqlConfig.js");/** * 查询列表页 */router.get(‘/‘, function (req, res, next) { sql.query(‘select * from admin_user‘, function (err, rows) { if (err) { console.log(err) res.render(‘index.ejs‘, { title: ‘Express‘, datas: [] }); } else { res.render(‘index.ejs‘, { title: ‘Express‘, datas: rows }); } })});module.exports = router;
The SQL folder continues to create the new adminusersql.js content as follows:
//admin_user表 sql语句var AdminUserSQL = { queryAll: ‘SELECT * FROM admin_user‘,};module.exports = AdminUserSQL;
Enter app.js and add the code:
//引入users模块var AdminUsersRouter = require(‘./routes/admin_user‘);app.use(‘/get-admin-users‘, AdminUsersRouter);
Start
Http://localhost:3000/get-admin-users
ExpressWelcome to ExpressuserName 姓名admin 超级管理员
Summarize
node. JS Mates Express connects MySQL to get a simple demo of the Admin_user table.
Communication Server Setup The first step is completed, next we will complete the message push with Socket.io. You are welcome to comment on the exchange.
SOURCE See GitHub
Node+express+socket.io+mysql= Communication Server Setup (i)