S2Ler / ExpressSwift

ExpressJS inspired web server framework on top of Swift NIO.
MIT License
1 stars 1 forks source link

accessing a public folder #3

Open gurugeek opened 4 years ago

gurugeek commented 4 years ago

I managed to get mustache and mongoDB to work with ExpressSwift, here is a sample route

express.use("/", .GET) { request, response in
do {
    let query : Document = ["slug": "index"]
         let documents = try collection.find (query)
         // check if an error occurred while iterating the cursor
         if let error = documents.error {    throw error    }
         var page : [String:String]    = [:]
         for d in documents {

             page = [
                 "title" : d.title,
                 "slug" : d.slug,
                 "body" : d.body,
                 "date" : d.date
             ]
             print (page)
            break

         }
    // check if an error occurred while iterating the cursor
    if let error = documents.error {
        throw error
    }

    let result = try! template.render(["page": page])
       response.headers.add(name: "Content-Type", value: "text/html; charset=utf-8")
  response.send(result)
}
catch {
    response.status = .internalServerError
    response.send(error.localizedDescription)
}
  return false
}

with a template like this

<h1> {{ page.title }} </h1>
<h1> {{{page.body}}}} </h1>

now body contains html code (which is intentionally not escaped note the 3 { on mustache) e.g. of images

 
<p>Notes can be shared through the share option so you can back them up, email them or send them via WhatsApp, messages, or any chat app. </p>

<br>

<img src="/public/screen1.png" alt="washnotes screenshot" style="width:300px;align:centre;">

<img src="/public/screen2.png"
alt="washnotes screenshot"
style="width:300px;align:centre;">

 

so I would like to know if there is a way to load these images from a /public folder :)

gurugeek commented 4 years ago

as a workaround Nginx can be configured to serve all files in the /Public folder

so I did this


## your public folder  note you need another folder inside the root folder to serve static 
## content so in case of everything in a /Public folder you need /Public/Public --> your files here

  root /home/gurugeek/tigerpress_swift/Public;      

    # pass requests for ExpressSwift 
    location / {
      proxy_pass      http://127.0.0.1:8080;
    }
## this can be obviously changed to whatever you want the folder to be e.g. /images 
location /Public {
    try_files $uri $uri/ @backend;
}

this would work fine (and perhaps be even more efficient).

S2Ler commented 4 years ago

@gurugeek Serving static content isn't supported in current version, but I find it will be a good addition. No ETA though.