Render html with node.js

Johanna Source

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:

index.html

<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>
    </div>
</body>

logincontroller

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

    $scope.open = function () {
        console.log('open i login.js');
        $http.get('/register')
    };
};

server.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.use(express.bodyParser());

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

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

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

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

Answers

answered 2 years ago psiphi75 #1

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) {
    res.render('register');
});

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:

html
  body(ng-app='')
    .container(ng-controller='LoginController')
      h1 Logg in
      input.form-control(placeholder='ID')
      input.form-control(placeholder='Password')
      button.btn Logga in
      button.btn(ng-click='open()') Register User

A couple of notes:

comments powered by Disqus