标签:style code http ext color get
app.js文件介绍,由于也是初学,下面的内容是个人的理解,有些不对的地方请评论中指证;以注解的形式说明。
//依赖组件[模块]导入
var express = require(‘express‘);
var path = require(‘path‘);
var favicon = require(‘static-favicon‘);
var logger = require(‘morgan‘);
var cookieParser = require(‘cookie-parser‘);
var bodyParser = require(‘body-parser‘);
var log = require(‘./logconfig‘); //log4js
log.use(app);
//路由部分,创建路由对象,设置定路由位置,指定一个路由文件;
var routes = require(‘./routes/index‘);
var users = require(‘./routes/users‘);
var weixin = require(‘./routes/weixin‘);
var chat =require(‘./routes/chat‘);
var myhome =require(‘./routes/myhome‘);
var edance=require(‘./routes/edance‘);
//app全局对象
var app = express();
//视图引擎指定,这是以ejs为例,还有很当类似ejs的模板引擎这里就不一一列出了
app.set(‘view engine‘,‘ejs‘);
app.set(‘views‘,path.join(__dirname + ‘/views‘));
//注册相关组件(模块)
app.use(favicon());
app.use(logger(‘dev‘));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, ‘public‘)));//设置express 项目的静态目录
//注册路由,与上面的路由对象一至,用于地址栏上访问路径,如:http://serever/users 这样的请求就会找到对应的路由,路由并处理相关请求并将结果渲染到视图层显示。
app.use(‘/‘, routes);
app.use(‘/users‘, users);
app.use(‘/weixin‘, weixin);
app.use(‘/chat‘, chat);
app.use(‘/myhome‘, myhome);
app.use(‘/edance‘,edance);
--------------------------可以加我微信号:googlecao 进行交流------------------------
/// catch 404 and forwarding to error handler
app.use(function(req, res, next) {
var err = new Error(‘Not Found‘);
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get(‘env‘) === ‘development‘) {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render(‘error‘, {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render(‘error‘, {
message: err.message,
error: {}
});
});
module.exports = app; //将app模块化,便于外部引用;
下一节讲路由的使用
学习node js 之微信公众帐号接口开发 准备工作之三,布布扣,bubuko.com
标签:style code http ext color get
原文地址:http://blog.csdn.net/jbboy/article/details/32913437