//引用
var express = require("express");
//应用中间件
var app = express();
//路由中间件
var router = express.Router();
//get\post请求路径
router.get('/', function (req, res, next) {
res.render('index', { title: 'Express' });
});
//获取参数
get请求 req.query.name
post请求 req.body.name
//指向页面index.hbs\index.html
res.render('index', { title: 'Express' });
//指向路由
res.redirect('/ceo_data');
//psot返回回json
res.json({success: 1,id: id});
//定义模块index.js方法一,index.js中
exports.getdata= function (req, res, next) {}
//定义模块index.js方法二,index.js中
var MATH = {
"pi": 3.14,
"e": 2.72,
};
module.exports = MATH;
//调用
var MATH = require("./MATH")
console.log(MATH.pi);
//引入模块index.js
var index= require('./routes/index');
router.get('/index/getdata', function(req, res, next) {
index.getdata(req, res, next);
});
//注册静态文件
app.use(express.static(path.join(__dirname, 'public')));
//注册404
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
//注册模板渲染
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
//sequelize数据库配置
var Sequelize = require('sequelize');
var my_db=new Sequelize(
"数据库",
"sa",
"pass123",{
dialect:'mysql',//mssql
host:'10.7.19.58',
port:3306
});
//NPM 安装
NPM install
//hbs模板配置
hbs.registerHelper('compare', function(left, operator, right, options) {
if (arguments.length < 3) {
throw new Error('Handlerbars Helper "compare" needs 2 parameters');
}
var operators = {
'==': function(l, r) {return l == r; },
'===': function(l, r) {return l === r; },
'!=': function(l, r) {return l != r; },
'!==': function(l, r) {return l !== r; },
'<': function(l, r) {return l < r; },
'>': function(l, r) {return l > r; },
'<=': function(l, r) {return l <= r; },
'>=': function(l, r) {return l >= r; },
'typeof': function(l, r) {return typeof l == r; }
};
if (!operators[operator]) {
throw new Error('Handlerbars Helper "compare" doesn\'t know the operator ' + operator);
}
var result = operators[operator](left, right);
if (result) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
//时间格式化
Date.prototype.format = function(fmt)
{
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt))
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
return fmt;
}
如果你遇到问题,可以加群315552185
一起交流哦!