Reputation: 313
I have been following this tutorial on building a web app and database using node.js, express and jade.
https://cozy.io/en/hack/getting-started/first-app.html
Despite laying out everything the same, my index.jade is not loading on localhost. There are no errors to suggest why on browser terminal. I have checked that my environment variables are set up, and I have altered the filepaths to index.jade, but it has not made any difference, just a white screen. On my command prompt, the server is listening and the database is connected.
My environment folder is
C:\foodshop and within this I have
node_modules,
index.jade,
package.json,
shopDB.db,
simpleserver.js
simpleserver.js contains the following -
// This is the server.
var http = require('http'),
express = require('express'),
app = express(),
sqlite3 = require('sqlite3').verbose(),
db = new sqlite3.Database('shopDB.db', (function(err) {
if (!err) {
console.log("Database is connected ... \n\n");
} else {
console.log("Error connecting database, check for shopDB.db file... \n\n");
}
}));
/* We add configure directive to tell express to use Jade to
render templates */
app.get('env', (function() {
app.set('views', __dirname + '');
app.engine('.html', require('jade').__express);
// Allows express to get data from POST requests
app.use(express.bodyParser());
}));
// Database initialization (First list names of tables and check if currently exists.)
db.get("SELECT name FROM sqlite_master WHERE type='table' AND name='itemList'", function(err, row) {
if (err !== null) {
console.log(err);
} else if (row == null) {
db.run('CREATE TABLE IF NOT EXISTS "itemList" ("ID" INTEGER PRIMARY KEY NOT NULL, "itemName" VARCHAR(100) NOT NULL, "itemWeight" INT(5) NOT NULL, "expiryDate" DATE, "itemPrice" double DEFAULT NULL)', function(err) {
if (err !== null) {
console.log(err);
} else {
console.log("SQL Table 'itemList' initialized.");
}
});
} else {
console.log("SQL Table 'itemList' already initialized.");
}
});
// We render the templates with the data
app.get('/', function(req, res) {
db.all('SELECT * FROM itemList ORDER BY itemName', function(err, row) {
if (err !== null) {
res.send(500, "An error has occurred -- " + err);
} else {
//res.sendfile('./public/index.html')
res.render('./index.jade', {
itemList: row
}, function(err, html) {
//res.sendfile('./index.jade')
//res.send(200, html);
res.status(200).send(html);
});
}
});
});
// We define a new route that will handle item creation
app.post('/add', function(req, res) {
ID = req.body.ID;
itemName = req.body.itemName;
itemWeight = req.body.itemWeight;
expiryDate = req.body.expiryDate;
itemPrice = req.body.itemPrice;
sqlRequest = "INSERT INTO 'itemList' (ID, itemName, itemWeight, expiryDate, itemPrice) VALUES('" + ID + "', '" + itemName + "', '" + itemWeight + "', '" + expiryDate + "', '" + itemPrice + "')"
db.run(sqlRequest, function(err) {
if (err !== null) {
res.send(500, "An error has occurred -- " + err);
} else {
res.redirect('back');
}
});
});
// We define another route that will handle item deletion
app.get('/delete/:itemName', function(req, res) {
db.run("DELETE FROM itemList WHERE itemName='" + req.params.itemName + "'", function(err) {
if (err !== null) {
res.send(500, "An error has occurred -- " + err);
} else {
res.redirect('back');
}
});
});
/* This will allow Cozy to run your app smoothly but
it won't break other execution environment */
var port = process.env.PORT || 9250;
var host = process.env.HOST || "127.0.0.1";
// Starts the server itself
var server = http.createServer(app).listen(port, host, function() {
console.log("Server listening to %s:%d within %s environment",
host, port, app.get('env'));
});
And this is the index.jade file
doctype 5
html(lang="en")
head
title Items
body
form(action="add", method="post")
label ID:
input(type="text", name="ID")
label itemName:
input(type="text", name="itemName")
label itemWeight:
input(type='text', name='itemWeight')
label expiryDate:
input(type='text', name='expiryDate')
label itemPrice:
input(type='text', name='itemPrice')
input(type="submit", value="Add a new item")
ul
- for(item in itemList) {
li
a(href=itemList[item].url)= itemList[item].itemName
| - (
a(href="delete/#{itemList[item].id}") delete
| )
- }
Upvotes: 1
Views: 622
Reputation: 1886
I followed that same tutorial and got to the part where they start with the Jade template and had the exact same problem you did. I backed up a little bit, and grabbed the example template from the Jade website, and it worked fine. I changed Cozy's "bookmark" template a little bit and got it working. You might try this:
doctype html
That different doctype made a difference in my example. I'm not an expert on Jade, but I'd definitely try that and see if you have any luck.
Edit: After looking a bit further, it looks like doctype 5
is deprecated, and doctype html
is recommended now.
Another Edit: If you're still having issues with your view rendering, I'd do two things. One, I'd check the tutorial out, match their jade view with yours, and start adding things one at a time until it breaks to narrow down the issue. Two, I'd change:
app.get('env', (function() {
app.set('views', __dirname + '');
app.engine('.html', require('jade').__express);
// Allows express to get data from POST requests
app.use(express.bodyParser());
}));
to
app.set('views', __dirname + '');
app.engine('.html', require('jade').__express);
// Allows express to get data from POST requests
app.use(express.bodyParser());
You don't appear to be using the env. variables, and I don't see any reason to move your view renderer setup inside that. Move it to the top (like in the example) and see if that works for you.
Upvotes: 7