Skip to content

Commit

Permalink
Support for single page apps
Browse files Browse the repository at this point in the history
  • Loading branch information
leo committed Jul 25, 2016
1 parent 2f64a13 commit 99f90e6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ You can find a list of all options [below](#options).
| -v, --version | The version tag of the micro-list instance on your device | - |
| -p, --port [port] | A custom port on which the app will be running | 3000 |
| -c, --cache [seconds] | How long static files should be cached in the browser | 3600 |
| -s, --single | Serve single page apps with only one index.html in the root directory | - |

## Contribute

Expand Down
29 changes: 25 additions & 4 deletions bin/list
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import toPromise from 'denodeify'
args
.option('port', 'Port to listen on', 3000)
.option('cache', 'How long static files should be cached in the browser (seconds)', 3600)
.option('single', 'Serve single page apps with only one index.html')

const flags = args.parse(process.argv)
const directory = args.sub[0]
Expand Down Expand Up @@ -79,6 +80,10 @@ const renderDirectory = async dir => {
let files = []
const subPath = path.relative(current, dir)

if (!await exists(dir)) {
return false
}

try {
files = await toPromise(fs.readdir)(dir)
} catch (err) {
Expand Down Expand Up @@ -172,16 +177,32 @@ const server = micro(async (req, res) => {

related = decodeURIComponent(path.format(related))

if (!await exists(related)) {
if (!await exists(related) && flags.single === undefined) {
return send(res, 404, 'Not found')
}

if (await isDir(related)) {
const indexPath = path.join(related, '/index.html')
// Check if file or directory path
if (path.parse(related).ext === '') {
let indexPath = path.join(related, '/index.html')

if (!await isDir(related) && flags.single === undefined) {
return send(res, 404, 'Not found')
}

res.setHeader('Content-Type', mime.lookup(indexPath) + '; charset=utf-8')

if (!await exists(indexPath)) {
return send(res, 200, renderDirectory(related))
const renderedDir = await renderDirectory(related)

if (renderedDir) {
return send(res, 200, renderedDir)
}

if (!flags.single) {
return send(res, 404, 'Not found')
}

indexPath = path.join(current, '/index.html')
}

let indexContent
Expand Down

2 comments on commit 99f90e6

@fabricionaweb
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature is awesome! <3 <3

Congrats, is the better static server

@leo
Copy link
Contributor Author

@leo leo commented on 99f90e6 Jul 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabricionaweb Glad to hear! 😊

Please sign in to comment.