Networked Media Week 4

Basic Database

With Node.js we can use a variety of database engines such as MySQL, Postgres, MongoDB, and so on.

nedb is a MongoDB compatible in memory or on disk datastore that is quick and easy for us to work with without going through a big setup process so let's get a start with databases there.

In order to use it, we have to install it as we would any normal server side node module:

npm install nedb

Here is some quick code to show you how it works. See the nedb documentation for more.

// Database to store data, don't forget autoload: true
var Datastore = require('nedb');
var db = new Datastore({filename: "data.db", autoload: true});
			

// Create a JavaScript Object with data to store
var datatosave = {
	name: "Shawn",
	message: "Hello world"
};
		
// Insert the data into the database
db.insert(datatosave, function (err, newDocs) {
	console.log("err: " + err);
	console.log("newDocs: " + newDocs);
});

// Find all of the existing docs in the database
db.find({}, function(err, docs) {
	// Loop through the results, send each one as if it were a new chat message
	for (var i = 0; i < docs.length; i++) {
		console.log(docs[i].name + " " + docs[i].message);
	}
});
			

Templates

As we have experienced, generating HTML within a node.js/Express server can be painful. Fortunately, there is a solution called templates which allow you to create your HTML as a separate file and have the Express template system bind data to your variables.

There are a plethora templating systems, many of which are cross-platform (moustache) and/or inspired by other platforms (jade/pug). These unfortunately are either complex to setup or require that you learn another language for authoring HTML. Fortunately, there is one system, ejs (embedded JavasScript) which allows regular HTML authoring with the ability to insert simple JavaScript statements which get run on the server side.

The first step to using ejs with Express is to install the module:

npm install ejs

To use ejs in our express app, we set the "view engine" to be "ejs" and then create a template to use

app.set('view engine', 'ejs');
Here is a basic template:
<!DOCTYPE html>
<html lang="en">
<body>
        <h1>My Cool Page</h1>
        <% if (person) {  %>
                <h2><%= person.name %><h2>
                <h3><%= person.other %></h3>
        <% } %>
</body>
</html>
save it as "template.ejs" inside a folder called "views" which is the default.

Then to use a template as the response to a request we do the following:

app.get('/templatetest', function(req, res) {
	var data = {person: {name: "Shawn", other: "blah"}};
    res.render('template.ejs', data);
});
The "render" function takes in the template file and binds the "data" to it. Of course, the "data" can be any data.

To render an array of data:

var data = {people: [{name: "Shawn", other: "blah"}, {name: "Joe", other: "No"}]};
you can use a "forEach" in the template:
<% people.forEach(function(person) {  %>
		<h2><%= person.name %><h2>
		<h3><%= person.other %></h3>
<% }); %>

More Information: Templating with EJS