Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/src/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ const { ifElse } = require('crocks/logic')
const { pipe } = require('crocks/helpers')
const { map } = require('crocks/pointfree')
const finalhandler = require('finalhandler')
const fs = require('fs')
const http = require('http')
const https = require('https')
const mime = require('mime')
const os = require('os')
const path = require('path')
const serveStatic = require('serve-static')
const { URL } = require('url')
const ws = require('ws')
const { update, readFile, notExists, writeFile, isObj, hasProp } = require('./utils')
const { fileNotFound, serverStarted } = require('./messages')
Expand Down Expand Up @@ -90,6 +92,7 @@ function getRequestType ({ pathname, fileType }, { proxyPrefix, pushstate }) {
return PROXY_TYPE
} else if (
isRoot({ pathname, pushstate, fileType }) ||
fileType === null ||
fileType === 'text/html'
) {
return CONTENT_TYPE
Expand All @@ -106,7 +109,8 @@ function getRequestType ({ pathname, fileType }, { proxyPrefix, pushstate }) {
* This function takes in the path from the request and the model and returns the needed values to finish running the request.
**/
function parseUrl (pathname, model) {
const fileType = pipe(path.extname, mime.getType)(pathname)
const fileExists = fs.existsSync(path.join(model.dir, pathname))
const fileType = fileExists ? pipe(path.extname, mime.getType)(pathname) : null
const requestType = getRequestType({ pathname, fileType }, model)
const rootPath = `${model.dir}/${model.startPage}`
const fullPath = `${model.dir}${pathname}`
Expand Down Expand Up @@ -172,9 +176,10 @@ function handler (model) {
const serve = serveStatic(model.dir, { index: ['index.html', 'index.htm'] })

return function (req, res) {
const url = parseUrl(req.url, model)
const url = new URL(req.url, `http://${req.headers.host}`)
const urlInfo = parseUrl(url.pathname, model)

if (proxy && url.isType(PROXY_TYPE)) {
if (proxy && urlInfo.isType(PROXY_TYPE)) {
req.url = req.url.replace(model.proxyPrefix, '')
proxy.web(req, res, {
target: model.proxyHost,
Expand All @@ -186,11 +191,11 @@ function handler (model) {
res.writeHead(502)
res.end('502 Bad Gateway')
})
} else if (url.isType(CONTENT_TYPE)) {
readFile(url.getPath, 'utf8')
.alt(readFile(url.rootpath, 'utf8'))
} else if (urlInfo.isType(CONTENT_TYPE)) {
readFile(urlInfo.getPath, 'utf8')
.alt(readFile(urlInfo.rootPath, 'utf8'))
.fork(resolveNotFound(res), resolveWith(model.reload, res))
} else if (url.isType(RELOAD_TYPE)) {
} else if (urlInfo.isType(RELOAD_TYPE)) {
readFile(RELOAD_FILE, 'utf8')
.map(file => file.replace(`'{{verbose}}'`, `${model.verbose}`))
.map(file => file.replace(`'{{reload}}'`, `${model.reload}`))
Expand Down