Serving static files with restify

前端 未结 7 1598
时光说笑
时光说笑 2020-12-29 20:26

I am learning to use Node.js. Currently, I have a folder structure that looks like the following:

index.html
server.js
client
  index.html
  subs
    index.h         


        
7条回答
  •  情书的邮戳
    2020-12-29 20:47

    According to my current restify version (v5.2.0)

    the serveStatic has been moved into plugins, so the code would be like this

    server.get(
      /\/(.*)?.*/,
      restify.plugins.serveStatic({
        directory: './static',
      })
    )
    

    Syntax above will serve your static files on folder static. So you can get the static file like http://yoursite.com/awesome-photo.jpg

    For some reason if you want to serve the static files under specific path like this http://yoursite.com/assets/awesome-photo.jpg for example.

    The code should be refactored into this

    server.get(
      /\/assets\/(.*)?.*/,
      restify.plugins.serveStatic({
        directory: `${app_root}/static`,
        appendRequestPath: false
      })
    )
    

    The option appendRequestPath: false above means we dont include assets path into the file name

提交回复
热议问题