Skip to content
This repository has been archived by the owner on Apr 30, 2019. It is now read-only.

How to serve static files outside of aero on root route '/'? #81

Closed
johnpittman opened this issue Mar 21, 2016 · 4 comments
Closed

How to serve static files outside of aero on root route '/'? #81

johnpittman opened this issue Mar 21, 2016 · 4 comments
Labels

Comments

@johnpittman
Copy link

Hey, thanks for taking the time to make this project.

I want to serve my index.html from a separate client project when I hit '/'. Is there something like express.static? Is the a method like sendFile for the response object?

Thank you!

@johnpittman johnpittman changed the title Config to use aero as api-only server? How to serve static files outside of aero on root route '/'? Mar 21, 2016
@akyoto
Copy link
Collaborator

akyoto commented Mar 22, 2016

Something like express.static is available if you specify a directory, e.g. images, inside the static array of your config.json file. Then the files inside images will be served under /images.

There is no sendFile function on the response object yet as I've only written Jade in my projects so far, never HTML directly.

You can however add a page with a controller (.js) that sends a file manually and route it to / via the url property in your .json file.

@johnpittman
Copy link
Author

"You can however add a page with a controller (.js) that sends a file manually"

  • Send the file manually by setting the response headers and reading from the file system using fs?

@akyoto
Copy link
Collaborator

akyoto commented Mar 23, 2016

I know it's annoying because sendFile() doesn't exist yet but something like this does work:

let serveJSFile = fileName => {
    return (request, response) => {
        let filePath = path.join(__dirname, fileName)
        let stat = fs.statSync(filePath)

        response.writeHead(200, {
            'Content-Type': 'application/javascript',
            'Content-Length': stat.size
        })

        fs.createReadStream(filePath).pipe(response)
    }
}

app.get('service-worker.js', serveJSFile('worker/service-worker.js'))
app.get('cache-polyfill.js', serveJSFile('worker/cache-polyfill.js'))

Note that this is just an example and it's not async. You can however easily create async controllers by promisify'ing the fs module and calling yield fs.statAsync(filePath) instead of fs.statSync.

@akyoto
Copy link
Collaborator

akyoto commented Mar 23, 2016

I implemented sendFile() in app and response in version 1.4.4, see #83 for usage information.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants