bloppit bloppit - 2 months ago 7
Javascript Question

Why is 'Cannot Get/' being shown for a simple render of pug?

Trying to set up a basic Express server with a basic pug template.

Can you please tell me what I'm doing wrong here?

'use strict';

//Require Express
var express = require('express');
var app = express();

//Require Pug
var pug = require('pug');

//Require Twitter
var Twitter = require('twitter');

//Set view engine to serve middleware
app.set('view engine', 'pug');

//Set where to look for templates
app.set('views', __dirname + '/templates');

//Set up style sheets
app.use('/static', express.static(__dirname + '/public'));

//Access keys to access twitter account
var config = {
"consumerKey": "",
"consumerSecret": "",
"accessToken": "",
"accessTokenSecret": ""
};

//instantiate twitter client
var client = new Twitter(config);

//Log whether
var error = function (err, response, body) {
console.log('ERROR [%s]', err);
};
var success = function (data) {
console.log('Data [%s]', data);
};

//Set up server on Port 3000
app.listen(3000, function() {
console.log("The frontend server is running on port 3000!");
});

//Render when appropriate
//Tell app to render template
app.get('/'), function(req, res){
res.render('index', {title: 'Hey', message: 'Hello there!'});
}


I'm getting back
The frontend server is running on port 3000!
in the console.

What am I missing?

I'd really appreciate any help please

Answer

You're calling app.get() wrong. You're doing

app.get('/'), function(req, res){
   ...

Which is two statements separated by the comma operator. The correct syntax is to pass the function as the second argument:

app.get('/', function(req, res){
   ...
});