Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
Rendering 100000 templates:
ECT
Escaped : 559ms
Unescaped : 134ms
Total : 693ms
Eco
Escaped : 2625ms
Unescaped : 491ms
Total : 3116ms
EJS
Escaped : 4621ms
Unescaped : 2786ms
Total : 7407ms
Handlebars.js
Escaped : 521ms
Unescaped : 275ms
Total : 796ms
Hogan.js
Escaped : 889ms
Unescaped : 720ms
Total : 1609ms
Swig
Escaped : 2052ms
Unescaped : 369ms
Total : 2421ms
Dust
Escaped : 553ms
Unescaped : 363ms
Total : 916ms
doT
Escaped : 1699ms
Unescaped : 100ms
Total : 1799ms
Fest
Escaped : 418ms
Unescaped : 202ms
Total : 620ms
Jade without `with`
Escaped : 4134ms
Unescaped : 3255ms
Total : 7389ms
Jade
Escaped : 13495ms
Unescaped : 12210ms
Total : 25705ms
— controllers (все объекты для работы с БД)
Тут есть маленькое отступление и сложность с которой я столкнулся. Если инициализацию сессий указывать после определения маршрутов, то сессии по какой-то причине на работают, по этому сессии инициализируем раньше.
app.use(errorNotFound);
function errorNotFound (req, res) {
var html = utils.acceptHtml(req);
// ф-ия acceptHtml смотрит заголовок запроса и -
// если true, то генерим ответ в виде html, иначе в json
if (html) {
res.writeHead(404);
res.end('error 404\n' + 'url ' + req.url + ' (' + req.method +') was not found on the server.');
// res.end по желанию можно заменить например на res.render('404', { params: params })
}
else {
res.json({ error: const.ERR_ROUTES_404 });
}
}
var someunit = function () {
"use strict";
// private переменные и методы
// ..
return {
// public переменные и методы
// ..
}();
module.exports = someunit;
var mem,
oldmem;
setInterval(function () {
// выводим кол-во занимаемой памяти только если оно изменилось с последнего замера
mem = process.memoryUsage().rss;
if (mem != oldmem) {
console.log('Memory usage, MB: ' + (mem / (1024 * 1024)).toFixed(3));
}
oldmem = mem;
}, const.MEM_OUTPUT_INTERVAL);
exports.getlogin = function(req, res){
// login
return;
}
exports.getlogin = function(req, res, next) {
// logic
if(err) next(new Error('My custom message error'));
}
// Error handling
app.use(function(err, req, res, next){
console.log(err);
if (err instanceof NotFound) {
res.render('errors/404', {
title : 'Not Found',
layout: 'login/layout',
status: 404
});
} else if (err instanceof AccessDenied) {
res.render('errors/403', {
title : 'Not Found',
layout: 'login/layout',
status: 403
});
} else if (err instanceof Unavailable) {
res.render('errors/503', {
title : 'Not Found',
layout: 'login/layout',
status: 503
});
} else if (err instanceof Offline) {
res.render('errors/offline', {
title : 'Offline',
layout: 'login/layout'
});
} else {
res.render('errors/500', {
title : 'The Server Encountered an Error',
layout: 'login/layout',
error: err,
status: 500
});
}
});
function AccessDenied(msg) {
this.name = 'AccessDenied';
Error.call(this, msg);
Error.captureStackTrace(this, arguments.callee);
}
function NotFound(msg) {
this.name = 'NotFound';
Error.call(this, msg);
Error.captureStackTrace(this, arguments.callee);
}
function Unavailable(msg) {
this.name = 'Unavailable';
Error.call(this, msg);
Error.captureStackTrace(this, arguments.callee);
}
function Offline(msg) {
this.name = 'Offline';
Error.call(this, msg);
Error.captureStackTrace(this, arguments.callee);
}
Изучение Node.js от начала до конца на практике. Часть 1