简体   繁体   中英

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:

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');

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM