UNPKG

wzq

Version:

微证券开发工具,集成ssi-server、构建等功能

61 lines (49 loc) 1.88 kB
var fs = require('fs'); var path = require('path'); var http = require('http'); var https = require('https'); var express = require('express'); var chalk = require('chalk'); var parseurl = require('parseurl'); var connectSSI = require('../connect-ssi'); var connectIndex = require('../connect-index'); var privateKey = fs.readFileSync(path.resolve(__dirname, './private.pem'), 'utf8'); var certificate = fs.readFileSync(path.resolve(__dirname, './file.crt'), 'utf8'); var credentials = {key: privateKey, cert: certificate}; var app = express(); var httpServer = http.createServer(app); var httpsServer = https.createServer(credentials, app); module.exports = function(config) { config = config || {}; if (!config.path) { console.log(chalk.red('working dir must be provided')); process.exit(); } if (!fs.existsSync(config.path)) { console.log(chalk.red('provided dir does not exists: ' + chalk.underline(config.path))); process.exit(); } config.port = config.port || 3000; app.use(function (req, res, next){ console.log(req.method.toUpperCase() + ': ' + chalk.underline(parseurl(req).pathname)); return next(); }); // html app.use(config.htmlPrefix || '', connectSSI({ baseDir: config.htmlPath || config.path, htmlPrefix: config.htmlPrefix })); app.use(config.cdnPrefix || '', express.static(config.path, { setHeaders: function(res, path) { res.setHeader('Access-Control-Allow-Origin', '*'); } })); // index app.use(connectIndex({ baseDir: config.path })); var Server = (config.protocol === 'http') ? httpServer : httpsServer; Server.listen(config.port, function () { console.log(`HTTP Server is running on port: ${config.port}, working dir: ${chalk.underline(config.path)}`); }); }