Search code examples
node.jsfirebasegoogle-cloud-functionshandlebars.jsinternal-server-error

http returns internal server error while using Firebase hosting and functions with node.js


I am getting an Internal Server Error from Firebase hosting and functions with node.js. Im following this tutorial and made sure my code is exactly the same as his: https://www.youtube.com/watch?v=LOeioOKUKI8&t=1s&pbjreload=101 I even have a database reference to facts.

Here is my index.js file:

const functions = require('firebase-functions');
const firebase = require('firebase-admin');
const express = require('express');
const engines = require('consolidate');

const firebaseApp = firebase.initializeApp(
    functions.config().firebase
);

function getFacts() {
    const ref = firebaseApp.database().ref('facts');
    return ref.once('value').then(snap => snap.val()); 
}

const app = express();
app.engine('hbs', engines.handlebars);
app.set('views', './views');
app.set('view engine', 'hbs');


app.get('/', (request, response) => {
    response.set('Cache-Control', 'public, max-age=300, s-maxage=600');
    getFacts().then(facts => {
        response.render('index', { facts });
    });
});

exports.app = functions.https.onRequest(app);

Here is my index.hbs file:

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>true facts.</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <h1>true facts.</h1>

    <ul>
        {{#each facts}}
        <li>{{text}}</li>
        {{each}}
    </ul>
</body>

</html>

Firebase.json file:

{
  "hosting": {
    "public": "public",
    "rewrites": [{
      "source": "**",
      "function": "app"
    }],
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}

database reference

internal_server_error

my_coding_folder

Firebase Functions log error message:

app
Error: Parse error on line 21: ...ul></body></html> --------------------^ Expecting 'OPEN_INVERSE_CHAIN', 'INVERSE', 'OPEN_ENDBLOCK', got 'EOF' at Parser.parseError 
(/srv/node_modules/handlebars/dist/cjs/handlebars/compiler/parser.js:267:19) at Parser.parse 
(/srv/node_modules/handlebars/dist/cjs/handlebars/compiler/parser.js:336:30) at parseWithoutProcessing 
(/srv/node_modules/handlebars/dist/cjs/handlebars/compiler/base.js:46:33) at HandlebarsEnvironment.parse 
(/srv/node_modules/handlebars/dist/cjs/handlebars/compiler/base.js:52:13) at compileInput 
(/srv/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:508:19) at ret 
(/srv/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:517:18) at /srv/node_modules/consolidate/lib/consolidate.js:935:16 at /srv/node_modules/consolidate/lib/consolidate.js:161:5 at Promise._execute 
(/srv/node_modules/bluebird/js/release/debuggability.js:384:9) at Promise._resolveFromExecutor 
(/srv/node_modules/bluebird/js/release/promise.js:518:18) at new Promise 
(/srv/node_modules/bluebird/js/release/promise.js:103:10) at promisify 
(/srv/node_modules/consolidate/lib/consolidate.js:154:10) at Function.exports.handlebars.render 
(/srv/node_modules/consolidate/lib/consolidate.js:925:10) at /srv/node_modules/consolidate/lib/consolidate.js:180:25 at readPartials 
(/srv/node_modules/consolidate/lib/consolidate.js:116:33) at /srv/node_modules/consolidate/lib/consolidate.js:174:7

Solution

  • add a / before the closing each in index.hbs

    {{/each}}