Mazzy
Mazzy

Reputation: 14227

Get route error in Node.js/Express

I get an error Cannot Get /. this is my folder structure

enter image description here

This is the route.js file:

//route.js

'use strict';

var app = require('../../config/express');

var router = app.Router();

/* Get Home Controller */
var homeController = require('../controllers/index');

router.get('/index', homeController.index); //it isn't recognized

app.use('/', router);

'use strict';

/*
 *  GET /
 *  Home Page
 */

exports.index = function(req, res){
  res.render('index', {
    'pageTitle': 'Express page'
  });
};

'use strict';

/* Import Express module */
var express = require('express');
var path = require('path');
//var bodyParser = require('body-parser');

/* Import env config parameters */
var settings = require('./env/settings');

/* Create express server */
var app = express();

/* Settings Application */
app.set('port', settings.port);
app.set('views', path.join(__dirname, '/frontend/views'));
app.set('view engine', 'jade');

//app.use(bodyParser.json());
//app.use(bodyParser.urlencoded({extended: true}));

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

module.exports = app;

I know that it is a problem on routing but I have tried to fix it

Upvotes: 0

Views: 945

Answers (1)

loganfsmyth
loganfsmyth

Reputation: 161637

Cannot Get / is exactly what is says. You have not defined any routes that match that path. You have defined /index, but not /, and they are two different URLs. index.html-style behavior is not provided by Express in routes. It is available with the static-file middleware if you want it though.

So change it to:

router.get('/', homeController.index);

or if you also want /index to work, just do both:

router.get('/', homeController.index);
router.get('/index', homeController.index);

Upvotes: 2

Related Questions