S2Ler/ExpressSwift

example with template rendering

gurugeek opened this issue · 9 comments

would you be interested (under payment/donation) to develop an example that includes rendering with a template like mustache for swift or similar?
I did manage to integrate MongoSwift as a database so it only misses the templating engine to be a full framework :)

S2Ler commented

I will investigate this out of curiosity :)

thank you 👍 I got mustache installed and (sort of) working
https://gist.github.com/gurugeek/4f2ab9d600a886dca63d755ee727eae4
the problem is the route /mustache returns

Luigi

I guess is because response.send doesn't parse the html ?

S2Ler commented

Can you show your template?

This library is very basic and doesn't have a lot of magic into it. If you send html, you would probably want to set "Content-Type" for example using:

  response.headers.add(name: "Content-Type", value: "text/html; charset=utf-8")

response.send just sends any string to the client. It is up to you.

The fact that /mustache returns <h1> Luigi </h1> is exactly what you told it to return. You need to modify your template to include more tags if needed.

I hope it will help you.

S2Ler commented

If you need more sophisticated library I would suggest to take a look on Vapor or Kitura :)

thanks a lot for your help ! what I was missing is response.headers and now it works fine with passing a simple variable !

Now I would be grateful for a little bit more help:

express.use("/", .GET) { request, response in
    let cursor = try! collection.find()
       var usernames = [String]() //empty array
       for document in cursor { // loop through mongocursor results
       let username = document.name
        let color = document.color
       usernames.append(username)
       usernames.append(color)
       }
    
  response.json(usernames)
  return false
}

this works but we get the data from the db as json obviously
["roscoe","orange","chester","tan"]

the idea was to pass it to a mustache template
that expect data in a format [String: Any] for example


let data: [String: Any] = [
    "name": "Arthur",
    "date": Date(),
    "realDate": Date().addingTimeInterval(60*60*24*3),
    "late": true
]

I tried


express.use("/", .GET) { request, response in
    let cursor = try! collection.find()
       var usernames = [String]() //empty array
       for document in cursor { // loop through mongocursor results
       let username = document.name
        let color = document.color
       usernames.append(username)
       usernames.append(color)
       }
    
  let result = try template.render (usernames)
       response.headers.add(name: "Content-Type", value: "text/html; charset=utf-8")
  response.send(result)
  return false
}

but I get

Invalid conversion from throwing function of type '(_, _) throws -> _' to non-throwing function type 'HandleFunc' (aka '(Request, Response) -> Bool')

sorry for if my question has more to do with my limited swift knowledge than your framework but I would appreciate your help -- very close to make it work with mustache + mongo db !

P.S. I am aware of vapor and kitura but I think your lightweight approach is way better !

S2Ler commented

Basically you need to handle try. Something like this:

express.use("/", .GET) { request, response in
do {
    let cursor = try collection.find()
       var usernames = [String]() //empty array
       for document in cursor { // loop through mongocursor results
       let username = document.name
        let color = document.color
       usernames.append(username)
       usernames.append(color)
       }
    
  let result = try template.render (usernames)
       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
}

thanks a lot ! mustache is now working fine :) will need to finish fighting with mongo and swift to get then right data and just render it. All also try with mysql. Thanks a lot !
P.S. if you have time for a small paid freelancing on this fun project let me know 👍

allright managed to get ExpressSwift to work with both MongoDB and mustache ! thanks a lot for your help.

P.S. is there an easy way to load public files for the Webserver e.g. images and css from a /public folder ? :) thanks again