Skip to content

accessing a public folder #3

@gurugeek

Description

@gurugeek

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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions