Skip to content
Go to file
Cannot retrieve contributors at this time
97 lines (78 sloc) 2.98 KB Express

Express is a module modelled after the ExpressJS framework. As usual in the module is supposed to be as similar as possible.

Express is a module based on top of Connect. It is also based on middleware, but adds additional routing features, support for templates (defaults to Mustache), and more fancy conveniences for JSON, etc ;-)

Show us some code! Some code from the express-simple example:

import express

let __dirname = process.cwd() // our modules have no __dirname

let app = express()

// Hook up middleware:
// - logging
// - POST form value decoder
// - cookie parsing (looks for cookies, adds them to the request)
// - session handling
// - serve files in 'public' directory
app.use(serveStatic(__dirname + "/public"))

// Setup dynamic templates: Mustache templates ending in .html in the
// 'views' directory.
app.set("view engine", "html") // really mustache, but we want to use .html
app.set("views", __dirname + "/views")

// Simple session based counter middleware:
app.use { req, _, next in
  req.session["viewCount"] = req.session[int: "viewCount"] + 1
  next() // just counting, no HTTP processing

// Basic form handling:
// - If the browser sends 'GET /form', render the
//   Mustache template in views/form.html.
// - If the browser sends 'POST /form', grab the values and render the
//   form with them
app.get("/form") { _, res, _ in
}"/form") { req, res, _ in
  let user = req.body[string: "user"] // form value 'user'
  let options : [ String : Any ] = [
    "user"      : user,
    "nouser"    : user.isEmpty
  res.render("form", options)

app.listen(1337) { print("Server listening: \($0)") }

A sample Mustache template:

{{> header}}    
  <form action="/form" method="POST">
      User <input name="user" type="text" placeholder="username"
                  value="{{user}}" />
      Password <input name="p" type="password" placeholder="password" />
    <input type="submit" class="button" value="Submit" />
{{> footer}}

This includes header/footer templates via {{> header}} and it renders values using {{user}}. You get the idea.

Examples comes with some Express examples:

  • express-simple (Mustache templates, forms, some JSON, cookies, session)
  • todo-mvc (A simple in-memory Todolist application backend aka TodoMVC)
  • todo-mvc-redis (A simple Todolist application backend aka TodoMVC which stores the data into Redis)
You can’t perform that action at this time.