Skip to content

Commit

Permalink
Refactor: es2016nify (#68)
Browse files Browse the repository at this point in the history
* Refactoring es2016nify
* simple code es2016nify
* fix test error
  • Loading branch information
segayuu authored and yoshinorin committed Nov 20, 2018
1 parent 9dc9413 commit 709ea4b
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 71 deletions.
4 changes: 2 additions & 2 deletions lib/middlewares/gzip.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

var compress = require('compression');
const compress = require('compression');

module.exports = function(app) {
var config = this.config.server || {};
const config = this.config.server || {};
if (!config.compress) return;

app.use(compress());
Expand Down
4 changes: 2 additions & 2 deletions lib/middlewares/header.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

module.exports = function(app) {
var config = this.config.server || {};
const config = this.config.server || {};
if (!config.header) return;

app.use(function(req, res, next) {
app.use((req, res, next) => {
res.setHeader('X-Powered-By', 'Hexo');
next();
});
Expand Down
8 changes: 4 additions & 4 deletions lib/middlewares/logger.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

var morgan = require('morgan');
const morgan = require('morgan');

module.exports = function(app) {
var config = this.config;
var args = this.env.args || {};
var logger = args.l || args.log || config.server.log;
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';
Expand Down
4 changes: 2 additions & 2 deletions lib/middlewares/redirect.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

module.exports = function(app) {
var root = this.config.root;
const { root } = this.config;
if (root === '/') return;

// If root url is not `/`, redirect to the correct root url
app.use(function(req, res, next) {
app.use((req, res, next) => {
if (req.method !== 'GET' || req.url !== '/') return next();

res.statusCode = 302;
Expand Down
23 changes: 11 additions & 12 deletions lib/middlewares/route.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
'use strict';

var pathFn = require('path');
var mime = require('mime');
const pathFn = require('path');
const mime = require('mime');

module.exports = function(app) {
var config = this.config;
var args = this.env.args || {};
var root = config.root;
var route = this.route;
const { config, route } = this;
const { args = {} } = this.env;
const { root } = config;

if (args.s || args.static) return;

app.use(root, function(req, res, next) {
var method = req.method;
app.use(root, (req, res, next) => {
const { method } = req;
if (method !== 'GET' && method !== 'HEAD') return next();

var url = route.format(decodeURIComponent(req.url));
var data = route.get(url);
var extname = pathFn.extname(url);
let url = route.format(decodeURIComponent(req.url));
const data = route.get(url);
const extname = pathFn.extname(url);

// When the URL is `foo/index.html` but users access `foo`, redirect to `foo/`.
if (!data) {
if (extname) return next();

url = encodeURI(url);
res.statusCode = 302;
res.setHeader('Location', root + url + '/');
res.setHeader('Location', `${root + url}/`);
res.end('Redirecting');
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/middlewares/static.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var serveStatic = require('serve-static');
const serveStatic = require('serve-static');

module.exports = function(app) {
app.use(this.config.root, serveStatic(this.public_dir, this.config.server.serveStatic));
Expand Down
83 changes: 35 additions & 48 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,77 @@
'use strict';

var connect = require('connect');
var http = require('http');
var chalk = require('chalk');
var Promise = require('bluebird');
var open = require('opn');
var net = require('net');
var url = require('url');
const connect = require('connect');
const http = require('http');
const chalk = require('chalk');
const Promise = require('bluebird');
const open = require('opn');
const net = require('net');
const url = require('url');

module.exports = function(args) {
var app = connect();
var config = this.config;
var ip = args.i || args.ip || config.server.ip || undefined;
var port = parseInt(args.p || args.port || config.server.port || process.env.port, 10) || 4000;
var root = config.root;
var self = this;
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(function() {
return self.extend.filter.exec('server_middleware', app, {context: self});
}).then(function() {
return checkPort(ip, port).then(() => this.extend.filter.exec('server_middleware', app, {context: this})).then(() => {
if (args.s || args.static) {
return self.load();
return this.load();
}

return self.watch();
}).then(function() {
return startServer(http.createServer(app), port, ip);
}).then(function(server) {
var addr = server.address();
var addrString = formatAddress(ip || addr.address, addr.port, root);
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);

self.log.info('Hexo is running at %s . Press Ctrl+C to stop.', chalk.underline(addrString));
self.emit('server');
this.log.info('Hexo is running at %s . Press Ctrl+C to stop.', chalk.underline(addrString));
this.emit('server');

if (args.o || args.open) {
open(addrString);
}

return server;
}).catch(function(err) {
}).catch(err => {
switch (err.code) {
case 'EADDRINUSE':
self.log.fatal('Port %d has been used. Try other port instead.', port);
this.log.fatal(`Port ${port} has been used. Try other port instead.`);
break;

case 'EACCES':
self.log.fatal('Permission denied. You can\'t use port ' + port + '.');
this.log.fatal(`Permission denied. You can't use port ${port}.`);
break;
}

self.unwatch();
this.unwatch();
throw err;
});
};

function startServer(server, port, ip) {
return new Promise(function(resolve, reject) {
server.listen(port, ip, function() {
resolve(server);
});

return new Promise((resolve, reject) => {
server.listen(port, ip, resolve);
server.on('error', reject);
});
}).then(() => server);
}

function checkPort(ip, port) {
return new Promise(function(resolve, reject) {
if (port > 65535 || port < 1) {
return reject(new RangeError('Port number ' + port + ' is invalid. Try a number between 1 and 65535.'));
}
if (port > 65535 || port < 1) {
return Promise.reject(new RangeError(`Port number ${port} is invalid. Try a number between 1 and 65535.`));
}

var server = net.createServer();
const server = net.createServer();

return new Promise((resolve, reject) => {
server.once('error', reject);

server.once('listening', function() {
server.close();
resolve();
});

server.once('listening', resolve);
server.listen(port, ip);
});
}).then(() => { server.close(); });
}

function formatAddress(ip, port, root) {
var hostname = ip;
let hostname = ip;
if (ip === '0.0.0.0' || ip === '::') {
hostname = 'localhost';
}
Expand Down

0 comments on commit 709ea4b

Please sign in to comment.