区块链技术博客
www.b2bchain.cn

Day6-程序主入口app求职学习资料

本文介绍了Day6-程序主入口app求职学习资料,有助于帮助完成毕业设计以及求职,是一篇很好的资料。

对技术面试,学习经验等有一些体会,在此分享。

主入口app.js

1:初始化express

var app = express();

2:设置日志

app.use(require('./logger/morgan'));

3:设置views

app.set('views', path.join(__dirname, 'views'));

4:设置express

//favicon.ico app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); //json解析 app.use(bodyParser.json()); //配置urlencoded app.use(bodyParser.urlencoded({ extended: false })); //配置cookie解析 app.use(cookieParser()); //配置public静态目录 app.use(express.static(path.join(__dirname, 'public'))); //开启cors app.use(cors()); app.use(requestIp.mw());

5:express router配置

//根目录 app.use('/', require('./routes/index')); //app api app.use('/app', require('./api/app')); //apidoc app.use('/api-doc', express.static(path.join(__dirname, 'apidoc')));

6:404处理

app.use(function(req, res, next) {   var err = new Error('Not Found');   err.status = 404;   next(err); });

7:拦截器错误处理

// error handlers app.use(function(err, req, res, next) {   if (err.name === 'JsonSchemaValidation') {     // Set a bad request http response status or whatever you want     res.status(400);      // Format the response body however you want     var responseData = {       statusText: 'Bad Request',       jsonSchemaValidation: true,       validations: err.validations // All of your validation information     };      // Take into account the content type if your app serves various content types     if (req.xhr || req.get('Content-Type') === 'application/json') {       res.json(responseData);     } else {       // If this is an html request then you should probably have       // some type of Bad Request html template to respond with       res.render('badrequestTemplate', responseData);     }   } else {     res.status(err.status || 500);     res.render('error', {       message: err.message,       error: app.get('env') === 'development' ? err : {} // print stacktrace for development     });   }  });

“`
/**

  • Main application file
    */

‘use strict’;

主入口app.js

1:初始化express

var app = express();

2:设置日志

app.use(require('./logger/morgan'));

3:设置views

app.set('views', path.join(__dirname, 'views'));

4:设置express

//favicon.ico app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); //json解析 app.use(bodyParser.json()); //配置urlencoded app.use(bodyParser.urlencoded({ extended: false })); //配置cookie解析 app.use(cookieParser()); //配置public静态目录 app.use(express.static(path.join(__dirname, 'public'))); //开启cors app.use(cors()); app.use(requestIp.mw());

5:express router配置

//根目录 app.use('/', require('./routes/index')); //app api app.use('/app', require('./api/app')); //apidoc app.use('/api-doc', express.static(path.join(__dirname, 'apidoc')));

6:404处理

app.use(function(req, res, next) {   var err = new Error('Not Found');   err.status = 404;   next(err); });

7:拦截器错误处理

// error handlers app.use(function(err, req, res, next) {   if (err.name === 'JsonSchemaValidation') {     // Set a bad request http response status or whatever you want     res.status(400);      // Format the response body however you want     var responseData = {       statusText: 'Bad Request',       jsonSchemaValidation: true,       validations: err.validations // All of your validation information     };      // Take into account the content type if your app serves various content types     if (req.xhr || req.get('Content-Type') === 'application/json') {       res.json(responseData);     } else {       // If this is an html request then you should probably have       // some type of Bad Request html template to respond with       res.render('badrequestTemplate', responseData);     }   } else {     res.status(err.status || 500);     res.render('error', {       message: err.message,       error: app.get('env') === 'development' ? err : {} // print stacktrace for development     });   }  });

“`
/**

  • Main application file
    */

‘use strict’;

主入口app.js

1:初始化express

var app = express();

2:设置日志

app.use(require('./logger/morgan'));

3:设置views

app.set('views', path.join(__dirname, 'views'));

4:设置express

//favicon.ico app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); //json解析 app.use(bodyParser.json()); //配置urlencoded app.use(bodyParser.urlencoded({ extended: false })); //配置cookie解析 app.use(cookieParser()); //配置public静态目录 app.use(express.static(path.join(__dirname, 'public'))); //开启cors app.use(cors()); app.use(requestIp.mw());

5:express router配置

//根目录 app.use('/', require('./routes/index')); //app api app.use('/app', require('./api/app')); //apidoc app.use('/api-doc', express.static(path.join(__dirname, 'apidoc')));

6:404处理

app.use(function(req, res, next) {   var err = new Error('Not Found');   err.status = 404;   next(err); });

7:拦截器错误处理

// error handlers app.use(function(err, req, res, next) {   if (err.name === 'JsonSchemaValidation') {     // Set a bad request http response status or whatever you want     res.status(400);      // Format the response body however you want     var responseData = {       statusText: 'Bad Request',       jsonSchemaValidation: true,       validations: err.validations // All of your validation information     };      // Take into account the content type if your app serves various content types     if (req.xhr || req.get('Content-Type') === 'application/json') {       res.json(responseData);     } else {       // If this is an html request then you should probably have       // some type of Bad Request html template to respond with       res.render('badrequestTemplate', responseData);     }   } else {     res.status(err.status || 500);     res.render('error', {       message: err.message,       error: app.get('env') === 'development' ? err : {} // print stacktrace for development     });   }  });

“`
/**

  • Main application file
    */

‘use strict’;

部分转自互联网,侵权删除联系

赞(0) 打赏
部分文章转自网络,侵权联系删除b2bchain区块链学习技术社区 » Day6-程序主入口app求职学习资料
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

b2b链

联系我们联系我们