我的电脑是mac电脑,所以只写mac电脑上的流程,关于windows上的流程其实也差不多了
1.去官网下载node js最新版本,并安装
[url]http://nodejs.cn/[/url]
2.去网上下载WebStorm,下载安装,地址忘了
3.用WebStorm建node js项目
4.下载插件
npm install ejs //这个是支持直接写html,用jade太郁闷了换
//npm install mysql //数据库连接(先不用这个用下面那个)
npm install libxmljs //读取xml
npm -install -g node-mysql //有数据库连接池
npm install express-session //增加session管理
npm install captchapng //生成验证码
sudo npm install log4js //增加日志输出框架
sudo npm install node-uuid //创建uuid
(如果要卸载用 npm uninstall xxx)
5.安装上之后修改app.js
配置好之后把所有jade后缀的文件修改成html后缀,完了文件中直接写html即可,输出js读取到的东西跟jsp中类似用<% ... %>输出
6.获取参数
HTTP GET : req.query.xxx
HTTP POST : req.body.xxx
7.读取xml
config.xml
dbHelperMysql.js
8.数据库连接(mysql)
使用示例:
9.session判断
在做后台的时候是需要过滤所有地址,必须用户登录之后才可访问
在app.js中配置
只要在登录之后往req.session.user中把用户对象存进去就行了
10.生成验证码
11.配置log4j
修改app.js
[quote]logger.trace(‘Entering cheese testing’);
logger.debug(‘Got cheese.’);
logger.info(‘Cheese is Gouda.’);
logger.warn(‘Cheese is quite smelly.’);
logger.error(‘Cheese is too ripe!’);
logger.fatal(‘Cheese was breeding ground for listeria.’);[/quote]
其它的后续研究添加
1.去官网下载node js最新版本,并安装
[url]http://nodejs.cn/[/url]
2.去网上下载WebStorm,下载安装,地址忘了
3.用WebStorm建node js项目
4.下载插件
npm install ejs //这个是支持直接写html,用jade太郁闷了换
//npm install mysql //数据库连接(先不用这个用下面那个)
npm install libxmljs //读取xml
npm -install -g node-mysql //有数据库连接池
npm install express-session //增加session管理
npm install captchapng //生成验证码
sudo npm install log4js //增加日志输出框架
sudo npm install node-uuid //创建uuid
(如果要卸载用 npm uninstall xxx)
5.安装上之后修改app.js
...
var ejs = require('ejs');
app.engine('html',ejs.__express);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
...
配置好之后把所有jade后缀的文件修改成html后缀,完了文件中直接写html即可,输出js读取到的东西跟jsp中类似用<% ... %>输出
6.获取参数
HTTP GET : req.query.xxx
HTTP POST : req.body.xxx
7.读取xml
config.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<db driver="mysql"
server="ip"
port="3306"
database="db"
user="root"
pwd="mysql" />
</root>
dbHelperMysql.js
/**
Author: iaiai
Date: 2014-6-19
Introduction: this module provides you with the operations (read) of xml file.
**/
function params(){
this.mydb_driver="";
this.mydb_server="";
this.mydb_database="";
this.mydb_user="";
this.mydb_pwd="";
}
function getParamObj(){
var libxmljs = require('libxmljs');
var fs = require('fs');
var path = './config/config.xml';
var param = new params();
var data = fs.readFileSync(path, 'utf8');
var xmlDoc = libxmljs.parseXmlString(data);
var mydb = xmlDoc.get('//db');
param.mydb_driver = mydb.attr('driver').value();
param.mydb_server = mydb.attr('server').value();
param.mydb_database = mydb.attr('database').value();
param.mydb_user = mydb.attr('user').value();
param.mydb_pwd = mydb.attr('pwd').value();
return param;
}
exports.getParamObj = getParamObj;
8.数据库连接(mysql)
/**
Author: iaiai(QQ:176291935)
Date: 2014-6-21
Introduction: this module provides you with the operations of add,del,update,select.
**/
//pre-required params
var mysql = require('mysql');
//open database
var pool = mysql.createPool({
host: "ip",
port: 3306,
user: "root",
password: "mysql",
database: "xxx"
});
//定义查询接口
exports.exec = function(sql,params,callback){
pool.getConnection(function(err,conn){
if(err){
callback(err,null,null);
}else{
conn.query(sql,params,function(qerr,values,fields){
//释放连接
conn.release();
//事件驱动回调
callback(qerr,values,fields);
});
}
});
};
使用示例:
var db = require('./config/db_mysql');
db.exec("select * from user",null,function(err,values,fields){
console.log("err:"+err);
console.log("values:"+values.length);
console.log("fields:"+fields);
for (var i=0;i<values.length;i++){
console.log(values[i]);
}
});
9.session判断
在做后台的时候是需要过滤所有地址,必须用户登录之后才可访问
在app.js中配置
...
//添加session模块
var session = require('express-session');
...
app.use(cookieParser());
添加session验证/
app.use(session({
resave: true, // don't save session if unmodified
saveUninitialized: false, // don't create session until something stored
secret: 'love'
}));
app.use(function(req,res,next){
//不过滤资源文件
if(req.url.match(/\.(jpg|css|js|png)$/i)!=null){
next();
return;
}
if (!req.session.user) {
if(req.url=="/login"){
next();//如果请求的地址是登录则通过,进行下一个请求
} else {
res.redirect('/login');
}
} else if (req.session.user) {
next();
}
});
结束session验证/
app.use(express.static(path.join(__dirname, 'public')));
...
只要在登录之后往req.session.user中把用户对象存进去就行了
10.生成验证码
var captchapng = require('captchapng');
var code = '0123456789';
var length = 4;
var randomcode = '';
for (var i = 0; i < length; i++) {
randomcode += code[parseInt(Math.random() * 1000) % code.length];
}
// 保存到session
if (null == req.session.code) {
req.session.code = {};
}
req.session.code = randomcode;
// 输出图片
var p = new captchapng(68,22,parseInt(randomcode)); // width,height,numeric captcha
p.color(255, 255, 255, 255); // First color: background (red, green, blue, alpha)
p.color(80, 80, 80, 255); // Second color: paint (red, green, blue, alpha)
var img = p.getBase64();
var imgbase64 = new Buffer(img,'base64');
res.writeHead(200, {
'Content-Type': 'image/png'
});
res.end(imgbase64);
11.配置log4j
修改app.js
var log4js = require('log4js');
log4js.configure({
appenders: [
{ type: 'console' }, //控制台输出
{
type: 'file', //文件输出
filename: 'logs/access.log',
maxLogSize: 1024,
backups:3,
category: 'normal'
}
]
});
var logger = log4js.getLogger('normal');
logger.setLevel('INFO');
...
//app.use(...)
//app.use(...)
app.use(log4js.connectLogger(logger, {level:log4js.levels.INFO}));
app.use(app.router);
[quote]logger.trace(‘Entering cheese testing’);
logger.debug(‘Got cheese.’);
logger.info(‘Cheese is Gouda.’);
logger.warn(‘Cheese is quite smelly.’);
logger.error(‘Cheese is too ripe!’);
logger.fatal(‘Cheese was breeding ground for listeria.’);[/quote]
其它的后续研究添加