1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| vim /hexo blog/node_modules/hexo-server/lib/server.js
'use strict';
const connect = require('connect'); const https = require('https'); const chalk = require('chalk'); const Promise = require('bluebird'); const open = require('open'); const net = require('net'); const url = require('url');
const fs = require('fs'); const express = require('express');
const httpApp = express();
httpApp.all('*', (req, res, next) => { let host = req.headers.host; host = host.replace(/\:\d+$/, ''); if (req.protocol === 'http') { res.redirect(307, `https://${host}${req.url}`); } else { next(); } });
const options = { key: fs.readFileSync('/rx90.cn.key'), cert: fs.readFileSync('/rx90.cn_bundle.crt') };
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 startServer(server, port, ip) { return new Promise((resolve, reject) => { server.listen(port, ip, resolve); server.on('error', reject); }).then(() => server); }
function formatAddress(ip, port, root) { let hostname = ip; if (ip === '0.0.0.0' || ip === '::') { hostname = 'localhost'; }
return url.format({protocol: 'http', hostname: hostname, port: port, path: root}); }
module.exports = function(args) { const app = connect(); const { config } = this; const ip = args.i || args.ip || config.server.ip || undefined; const defaultPort = 4000; const port = parseInt(args.p || args.port || config.server.port || process.env.port || defaultPort, 10); const { root } = config;
return checkPort(ip, port) .then(() => { const httpsServer = https.createServer(options, app);
const httpRedirectServer = httpApp.listen(80, () => { console.log(`HTTP redirect server listening on port 80, redirecting to HTTPS`); });
return this.extend.filter.exec('server_middleware', app, {context: this}) .then(() => { if (args.s || args.static) { return this.load(); }
return this.watch(); }) .then(() => startServer(httpsServer, 443, ip)) .then(server => { const addrString = formatAddress(ip || 'localhost', 443, root); 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(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; }); };
|