first commit
This commit is contained in:
10
node_modules/hexo-server/lib/middlewares/gzip.js
generated
vendored
Normal file
10
node_modules/hexo-server/lib/middlewares/gzip.js
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
const compress = require('compression');
|
||||
|
||||
module.exports = function(app) {
|
||||
const config = this.config.server || {};
|
||||
if (!config.compress) return;
|
||||
|
||||
app.use(compress());
|
||||
};
|
||||
11
node_modules/hexo-server/lib/middlewares/header.js
generated
vendored
Normal file
11
node_modules/hexo-server/lib/middlewares/header.js
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function(app) {
|
||||
const config = this.config.server || {};
|
||||
if (!config.header) return;
|
||||
|
||||
app.use((req, res, next) => {
|
||||
res.setHeader('X-Powered-By', 'Hexo');
|
||||
next();
|
||||
});
|
||||
};
|
||||
14
node_modules/hexo-server/lib/middlewares/logger.js
generated
vendored
Normal file
14
node_modules/hexo-server/lib/middlewares/logger.js
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
const morgan = require('morgan');
|
||||
|
||||
module.exports = function(app) {
|
||||
const { config } = this;
|
||||
const { args = {} } = this.env;
|
||||
let logger = args.l || args.log || config.server.log;
|
||||
|
||||
if (!logger && !args.debug) return;
|
||||
if (typeof logger !== 'string') logger = 'dev';
|
||||
|
||||
app.use(morgan(logger));
|
||||
};
|
||||
15
node_modules/hexo-server/lib/middlewares/redirect.js
generated
vendored
Normal file
15
node_modules/hexo-server/lib/middlewares/redirect.js
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function(app) {
|
||||
const { root } = this.config;
|
||||
if (root === '/') return;
|
||||
|
||||
// If root url is not `/`, redirect to the correct root url
|
||||
app.use((req, res, next) => {
|
||||
if (req.method !== 'GET' || req.url !== '/') return next();
|
||||
|
||||
res.statusCode = 302;
|
||||
res.setHeader('Location', root);
|
||||
res.end('Redirecting');
|
||||
});
|
||||
};
|
||||
87
node_modules/hexo-server/lib/middlewares/route.js
generated
vendored
Normal file
87
node_modules/hexo-server/lib/middlewares/route.js
generated
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
'use strict';
|
||||
|
||||
const pathFn = require('path');
|
||||
const mime = require('mime');
|
||||
|
||||
module.exports = function(app) {
|
||||
const { config, route } = this;
|
||||
const { args = {} } = this.env;
|
||||
const { root, feed } = config;
|
||||
|
||||
if (args.s || args.static) return;
|
||||
|
||||
const { pretty_urls } = config;
|
||||
const { trailing_index, trailing_html } = pretty_urls ? pretty_urls : {};
|
||||
|
||||
app.use(root, (req, res, next) => {
|
||||
const { method, url: requestUrl } = req;
|
||||
if (method !== 'GET' && method !== 'HEAD') return next();
|
||||
|
||||
let url = route.format(decodeURIComponent(requestUrl));
|
||||
let data = route.get(url);
|
||||
let extname = pathFn.extname(url);
|
||||
|
||||
if (!data) {
|
||||
if (route.get(url + '.html')) {
|
||||
// location `foo/bar.html`; request `foo/bar`; proxy to the location
|
||||
extname = '.html';
|
||||
data = route.get(url + extname);
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
req.url = encodeURI('/' + url + extname);
|
||||
data.pipe(res).on('error', next);
|
||||
return;
|
||||
} else if (route.get(url + '/index.html')) {
|
||||
// location `foo/index.html`; request `foo`; redirect to `foo/`
|
||||
url = encodeURI(url);
|
||||
res.statusCode = 301;
|
||||
res.setHeader('Location', `${root + url}/`);
|
||||
res.end('Redirecting');
|
||||
return;
|
||||
} else if (route.get(url.replace(/\/index\.html$/i, '') + '.html')) {
|
||||
// location `foo/bar.html`; request `foo/bar/`; redirect to `foo`
|
||||
// request with trailing slash is appended with index.html by route.format()
|
||||
url = encodeURI(url.replace(/\/index\.html$/i, ''));
|
||||
res.statusCode = 301;
|
||||
res.setHeader('Location', root + url);
|
||||
res.end('Redirecting');
|
||||
return;
|
||||
} return next();
|
||||
}
|
||||
if (trailing_html === false && !requestUrl.endsWith('/index.html') && requestUrl.endsWith('.html')) {
|
||||
// location `foo/bar.html`; request `foo/bar.html`; redirect to `foo/bar`
|
||||
url = encodeURI(url.replace(/\.html$/i, ''));
|
||||
res.statusCode = 301;
|
||||
res.setHeader('Location', root + url);
|
||||
res.end('Redirecting');
|
||||
return;
|
||||
} else if (trailing_index === false && requestUrl.endsWith('/index.html')) {
|
||||
// location `foo/index.html`; request `foo/index.html`; redirect to `foo/`
|
||||
url = encodeURI(url.replace(/index\.html$/i, ''));
|
||||
res.statusCode = 301;
|
||||
res.setHeader('Location', root + url);
|
||||
res.end('Redirecting');
|
||||
return;
|
||||
}
|
||||
|
||||
// Do not overwrite header if already set by another middleware
|
||||
if (!res.hasHeader('Content-Type')) {
|
||||
if (feed && feed.path === url) {
|
||||
if (feed.type === 'atom') {
|
||||
res.setHeader('Content-Type', 'application/atom+xml');
|
||||
} else if (feed.type === 'rss') {
|
||||
res.setHeader('Content-Type', 'application/rss+xml');
|
||||
} else {
|
||||
res.setHeader('Content-Type', extname ? mime.getType(extname) : 'application/octet-stream');
|
||||
}
|
||||
} else {
|
||||
res.setHeader('Content-Type', extname ? mime.getType(extname) : 'application/octet-stream');
|
||||
}
|
||||
}
|
||||
|
||||
if (method === 'GET') {
|
||||
data.pipe(res).on('error', next);
|
||||
} else {
|
||||
res.end();
|
||||
}
|
||||
});
|
||||
};
|
||||
7
node_modules/hexo-server/lib/middlewares/static.js
generated
vendored
Normal file
7
node_modules/hexo-server/lib/middlewares/static.js
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const serveStatic = require('serve-static');
|
||||
|
||||
module.exports = function(app) {
|
||||
app.use(this.config.root, serveStatic(this.public_dir, this.config.server.serveStatic));
|
||||
};
|
||||
80
node_modules/hexo-server/lib/server.js
generated
vendored
Normal file
80
node_modules/hexo-server/lib/server.js
generated
vendored
Normal file
@@ -0,0 +1,80 @@
|
||||
'use strict';
|
||||
|
||||
const connect = require('connect');
|
||||
const http = require('http');
|
||||
const { underline } = require('picocolors');
|
||||
const Promise = require('bluebird');
|
||||
const open = require('open');
|
||||
const net = require('net');
|
||||
|
||||
module.exports = function(args) {
|
||||
const app = connect();
|
||||
const { config } = this;
|
||||
const ip = args.i || args.ip || config.server.ip || undefined;
|
||||
const port = parseInt(args.p || args.port || config.server.port || process.env.port, 10) || 4000;
|
||||
const { root } = config;
|
||||
|
||||
return checkPort(ip, port).then(() => this.extend.filter.exec('server_middleware', app, {context: this})).then(() => {
|
||||
if (args.s || args.static) {
|
||||
return this.load();
|
||||
}
|
||||
|
||||
return this.watch();
|
||||
}).then(() => startServer(http.createServer(app), port, ip)).then(server => {
|
||||
const addr = server.address();
|
||||
const addrString = formatAddress(ip || addr.address, addr.port, root);
|
||||
|
||||
this.log.info('Hexo is running at %s . Press Ctrl+C to stop.', underline(addrString));
|
||||
this.emit('server');
|
||||
|
||||
if (args.o || args.open) {
|
||||
open(addrString);
|
||||
}
|
||||
|
||||
return server;
|
||||
}).catch(err => {
|
||||
switch (err.code) {
|
||||
case 'EADDRINUSE':
|
||||
this.log.fatal(`Port ${port} has been used. Try other port instead.`);
|
||||
break;
|
||||
|
||||
case 'EACCES':
|
||||
this.log.fatal(`Permission denied. You can't use port ${port}.`);
|
||||
break;
|
||||
}
|
||||
|
||||
this.unwatch();
|
||||
throw err;
|
||||
});
|
||||
};
|
||||
|
||||
function startServer(server, port, ip) {
|
||||
return new Promise((resolve, reject) => {
|
||||
server.listen(port, ip, resolve);
|
||||
server.on('error', reject);
|
||||
}).then(() => server);
|
||||
}
|
||||
|
||||
function checkPort(ip, port) {
|
||||
if (port > 65535 || port < 1) {
|
||||
return Promise.reject(new RangeError(`Port number ${port} is invalid. Try a number between 1 and 65535.`));
|
||||
}
|
||||
|
||||
const server = net.createServer();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
server.once('error', reject);
|
||||
server.once('listening', resolve);
|
||||
server.listen(port, ip);
|
||||
}).then(() => { server.close(); });
|
||||
}
|
||||
|
||||
function formatAddress(ip, port, root) {
|
||||
let hostname = ip;
|
||||
if (ip === '0.0.0.0' || ip === '::') {
|
||||
hostname = 'localhost';
|
||||
}
|
||||
|
||||
let path = root.startsWith("/") ? root : `/${root}`;
|
||||
return new URL(`http://${hostname}:${port}${path}`).toString();
|
||||
}
|
||||
Reference in New Issue
Block a user