Giter VIP home page Giter VIP logo

Comments (4)

akyoto avatar akyoto commented on July 28, 2024 1

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.

from aero.

akyoto avatar akyoto commented on July 28, 2024 1

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

from aero.

akyoto avatar akyoto commented on July 28, 2024

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.

from aero.

johnpittman avatar johnpittman commented on July 28, 2024

"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?

from aero.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.