cnpmjs.org
Version:
Private npm registry and web for Enterprise, base on MySQL and Simple Store Service
54 lines (45 loc) • 1 kB
JavaScript
/**!
* cnpmjs.org - middleware/web_not_found.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/
;
/**
* Module dependencies.
*/
var debug = require('debug')('cnpmjs.org:middleware:web_not_found');
module.exports = function* notFound(next) {
yield* next;
if (this.status && this.status !== 404) {
return;
}
if (this.body) {
return;
}
var m = /^\/([\w\-\.]+)\/?$/.exec(this.path);
if (!m) {
// scoped packages
m = /^\/(@[\w\-\.]+\/[\w\-\.]+)$/.exec(this.path);
}
debug('%s match %j', this.url, m);
if (m) {
return this.redirect('/package/' + m[1]);
}
// package not found
m = /\/package\/([\w\-\_\.]+)\/?$/.exec(this.url);
var name = null;
var title = '404: Page Not Found';
if (m) {
name = m[1];
title = name + ' Not Found';
}
this.status = 404;
yield* this.render('404', {
title: title,
name: name
});
};