Skip to content

Instantly share code, notes, and snippets.

@ArnauAregall
Created September 6, 2013 17:18
Show Gist options
  • Save ArnauAregall/6466858 to your computer and use it in GitHub Desktop.
Save ArnauAregall/6466858 to your computer and use it in GitHub Desktop.
var express = require('express'),
consolidate = require('consolidate');
var app = express();
app.engine("html", consolidate.swig); // setup template engine Swig
app.set('view engine', 'html'); // the Swig engine will render HTML
app.set('views', __dirname); // and the HTML views are in this folder
/*
Note: The proper way to do this would be:
app.set('views', __dirname + "/views");
*/
// http://localhost:8080/
app.get("/", function(req, res) {
res.render("hello", {"name" : "Express Server"});
});
// Handling a GET request
// http://localhost/<name>
app.get("/:name", function(req, res) {
var name = req.params.name; // the GET parameter value
res.render("hello", {"name" : name});
});
// Error handling
app.get("*", function(res, res) {
res.send("Page not found", 404);
});
app.listen(8080); // start our ExpressJS server on port 8080
console.log("Express server started on localhost 8080");
<html>
<head>
<title>NodeJS + ExpressJS + Swig (Hello World)</title>
</head>
<body>
<h1>Hello, my name is {{name}}</h1>
</body>
</html>
{
"name" : "NodeJS_ExpressJS_Swig_HelloWorld",
"version" : "0.0.0",
"description" : "A simple Hello World app with NodeJS, ExpressJS server and Swig template engine.",
"main" : "app.js",
"dependencies" : {
"consolidate" : "~0.9.1",
"express" : "~3.2.6"
},
"author" : "ArnauAregall",
"license" : "BSD"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment