Johanna Johanna - 1 year ago 278
HTML Question

Render html with node.js

I am really new to the whole MEAN-stack and are trying to create an application on openshift but are unable to render a new page.

I just keep getting this error and can't solve it with anything I've googled.

My Error: Failed to lookup view "/register" in public directory

It works completely fine to render the index page with app.get('/', func()) in server.js and tried to to the exact same thing with app.get('/register). I used to have the same problem with '/' at first but solved it using app.use(express.static(__dirname + '/public'));

Both index.html and register.html are located in the public directory.

These are extracts of my code:


<body ng-app="">
<div class="container" ng-controller="LoginController" >
<h1>Logg in</h1>
<input class="form-control" placeholder="ID"/>
<input class="form-control" placeholder="Password"/>
<button class="btn">Logga in</button>
<button ng-click="open()" class="btn">Register User</button>


function LoginController($scope, $http) {
console.log("Hello from Login");

$ = function () {
console.log('open i login.js');


var express = require('express');
var fs = require('fs');
var mongojs = require('mongojs');
var jade = require('jade')

var app = express();
var cors = require('cors');
var bodyParser = require('body-parser');

app.use(express.static(__dirname + '/public'));

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

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

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

app.set('view engine', 'jade');

Answer Source

There are a couple of issues.

1) Don't use a slash for the 'register' file. This is a file in the /public folder, not a folder or route.

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

2) You have set jade as your rendering engine. This means you will be serving .jade files. Your public folder should have index.jade. And it should look like this:

      h1 Logg in
      button.btn Logga in
      button.btn(ng-click='open()') Register User

A couple of notes:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download