0

I have written a server named "app.js" which display many pages of my web site.

var http=require("http");
var express = require('express');
var app = express();
var server = http.createServer(app);
var ejs = require('ejs');
var path = require('path');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
    // routes
var route = require('./route');



app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

server.listen(3000,function(){
console.log("server listen at localhost:3000");});
app.get('/', route.index);

route.js:

var index = function(req, res, next) {
   if(!req.isAuthenticated()) {
      res.redirect('/index');
   } else {

      var user = req.user;

      if(user !== undefined) {
         user = user.toJSON();
      }
      res.render('menu', {title: 'Home', user: user});
   }
};
module.exports.index = index;

The folder of project:

/interface
      /views
            index.ejs
      app.js
      route.js

How can I fix it ? what is the mistake in my program?

4
  • I'd try sending the index file without route and without ejs first. Commented May 24, 2016 at 19:52
  • Do you mean that maybe the problem from file index?? Commented May 24, 2016 at 19:58
  • Atm, the problem can come from ejs, router, rendering, authenfication system and/or express. I'd send the index file using only express. If it works, add authenfication, then if it works, add ejs. Commented May 24, 2016 at 20:01
  • I tried this:app.get('/', function(req, res) { res.render(path.join(__dirname + '/views/index.ejs')); });I got the page without css style. Commented May 24, 2016 at 20:07

1 Answer 1

1

Your app.get is unreachable after server.listen.

Place it before:

app.js:

var http=require("http");
var express = require('express');
var app = express();
var server = http.createServer(app);
var ejs = require('ejs');
var path = require('path');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
    // routes
var route = require('./route');



app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// here:
app.get('/', route.index); 

server.listen(3000,function(){
console.log("server listen at localhost:3000");});
// app.get('/', route.index); <= not here

When not authenticated, redirection is not what you want, but:

route.js:

// res.redirect('/index');
res.render('index');
Sign up to request clarification or add additional context in comments.

4 Comments

Could you give us error details? It will be easier to fix it then.
I just got cannot get /index
Maybe the problem is with "app.set('views', path.join(__dirname, 'views'));" because when I wrote app.get('/', function(req, res) { res.render(path.join(__dirname + '/views/index.ejs')); }); I got the page.
res.redirect('/index'); won't render but redirect it explains you cannot get /index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.