siegel
Version:
Web application development ecosystem
45 lines (44 loc) • 1.88 kB
JavaScript
import https from 'https';
import express from 'express';
import { HEADER_ACCEPT_INDEX } from './constants.js';
import extractSSL from './extract_ssl_key.js';
import getStaticServingData from './get_static_file_response_data/index.js';
const rewriteSPAUrl = (req, _, next) => {
const { headers, method } = req;
if (method == 'GET' && headers.accept?.includes(HEADER_ACCEPT_INDEX)) {
req.url = '/index.html';
}
next();
};
async function createHTTPServer(params) {
const { devMiddlewares, config } = params;
const { publicDir, server } = config;
const { ssl, serveCompressionsPriority, appServer, HTTP1PreFileSend } = server;
const staticServer = express();
appServer && await appServer({ staticServer, express }, config);
staticServer.disable('x-powered-by')
.use(rewriteSPAUrl);
devMiddlewares.length
? devMiddlewares.forEach(m => {
staticServer.use(m);
})
: staticServer.use((req, res) => {
const { url, headers } = req;
const staticServingData = getStaticServingData({
serveCompressionsPriority,
publicDir: publicDir,
reqUrl: url,
acceptEncoding: headers['accept-encoding']?.toString(),
cacheControl: headers['cache-control']
});
const { pathToFile, encoding, contentType, cacheControl } = staticServingData;
encoding && res.append('content-encoding', encoding);
contentType && res.append('content-type', contentType);
cacheControl && res.append('cache-control', cacheControl);
HTTP1PreFileSend?.(req, res, staticServingData) || res.sendFile(pathToFile);
});
return ssl
? https.createServer(extractSSL(ssl), staticServer)
: staticServer;
}
export default createHTTPServer;