0

Im new at nodejs programming and im having a little problem now. When i try to go localhost:3000/ i want to go homeController and index function prints the HTML file.

APP.JS

const express = require('express')
const mongoose = require('mongoose')
const mongodb = require('mongodb')

const app = express();

const homeController = require('./home/homeController.js');


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

app.listen(3000, () => console.log('Example app listening on port 80!'))

HOMECONTROLLER.JS

var path    = require("path");

exports.index = function(req, res){
  res.sendFile(path.join(__dirname+'/index.html'));
};

console.log('test33');

Also i am using exports to seperate app.js from other controllers. Is this the right way? I have a history with Python Django framework and we used to use URLs to navigate our program.

Thanks.

OUTPUT

Cannot GET /function%20(req,%20res)%7B%0A%20%20res.sendFile(path.join(__dirname+'/index.html'));%0A%7D

2 Answers 2

2

Your problem is that homeController.index is a function, but you're not calling it. Replace:

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

with:

app.get('/', homeController.index);
Sign up to request clarification or add additional context in comments.

Comments

1

Your homeController.js exports an index function which requires two parameters reqand res. So you have to update your app.js accordingly :

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

edit: by the way, your app is listening to port 3000

Comments

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.