简体   繁体   English

在Node.js / Express中获取路由错误

[英]Get route error in Node.js/Express

I get an error Cannot Get / . 我收到错误消息Cannot Get / this is my folder structure 这是我的文件夹结构

在此处输入图片说明

This is the route.js file: 这是route.js文件:

//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 我知道这是路由问题,但我已尝试解决

Cannot Get / is exactly what is says. Cannot Get /恰恰是所说的。 You have not defined any routes that match that path. 您尚未定义任何与该路径匹配的路由。 You have defined /index , but not / , and they are two different URLs. 您已经定义/index ,但不是/ ,它们是两个不同的网址。 index.html -style behavior is not provided by Express in routes. Express在路由中未提供index.html -style行为。 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: 或者,如果您也希望/index起作用,则只需执行以下两项操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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