From 709ea4b259507002a05eb2681b83e4e9ae73531d Mon Sep 17 00:00:00 2001 From: segayuu Date: Tue, 20 Nov 2018 19:28:37 +0900 Subject: [PATCH] Refactor: es2016nify (#68) * Refactoring es2016nify * simple code es2016nify * fix test error --- lib/middlewares/gzip.js | 4 +- lib/middlewares/header.js | 4 +- lib/middlewares/logger.js | 8 ++-- lib/middlewares/redirect.js | 4 +- lib/middlewares/route.js | 23 +++++----- lib/middlewares/static.js | 2 +- lib/server.js | 83 ++++++++++++++++--------------------- 7 files changed, 57 insertions(+), 71 deletions(-) diff --git a/lib/middlewares/gzip.js b/lib/middlewares/gzip.js index 1408a97..9b0f53f 100644 --- a/lib/middlewares/gzip.js +++ b/lib/middlewares/gzip.js @@ -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()); diff --git a/lib/middlewares/header.js b/lib/middlewares/header.js index fdebeb7..f623846 100644 --- a/lib/middlewares/header.js +++ b/lib/middlewares/header.js @@ -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(); }); diff --git a/lib/middlewares/logger.js b/lib/middlewares/logger.js index e1ac3d0..611eb1c 100644 --- a/lib/middlewares/logger.js +++ b/lib/middlewares/logger.js @@ -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'; diff --git a/lib/middlewares/redirect.js b/lib/middlewares/redirect.js index 8bcaf03..acb3cd2 100644 --- a/lib/middlewares/redirect.js +++ b/lib/middlewares/redirect.js @@ -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; diff --git a/lib/middlewares/route.js b/lib/middlewares/route.js index c14fdab..8ae70e0 100644 --- a/lib/middlewares/route.js +++ b/lib/middlewares/route.js @@ -1,23 +1,22 @@ '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) { @@ -25,7 +24,7 @@ module.exports = function(app) { url = encodeURI(url); res.statusCode = 302; - res.setHeader('Location', root + url + '/'); + res.setHeader('Location', `${root + url}/`); res.end('Redirecting'); return; } diff --git a/lib/middlewares/static.js b/lib/middlewares/static.js index 3a18db4..19ba369 100644 --- a/lib/middlewares/static.js +++ b/lib/middlewares/static.js @@ -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)); diff --git a/lib/server.js b/lib/server.js index 72b2c56..982cf97 100644 --- a/lib/server.js +++ b/lib/server.js @@ -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'; }