NodeJS无法找到路由

问题描述 投票:0回答:3

更新了Server.js代码:

/server.就是

var express = require('express');

var app = express();
var bodyParser = require('body-parser');

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

app.use(express.static(__dirname + "/public"));
app.set('views', path.join(__dirname, 'views'));

var indexRoutes = require('./routes/index')
app.use('/', indexRoutes);

app.use(bodyParser.json()); 

app.listen(3000);
console.log("yep >>> Server running on port 3000");

我仍然没有找到相同的错误

结束更新

我正在尝试在nodejs中设置路由,并且能够显示index.html页面,但是当我点击关于链接时,我得到以下错误:错误:

错误:无法在ServerResponse.res的EventEmitter.app.render(c:\ nodejs \ node_modules \ express \ lib \ application.js:519:17)的视图目录“c:\ nodejs \ views”中查找视图“about” .render(c:\ nodejs \ node_modules \ express \ lib \ response.js:933:7)在Exports.about(c:\ nodejs \ routes \ index.js:11:9)的Layer.handle [as handle_request] (c:\ nodejs \ node_modules \ express \ lib \ router \ layer.js:82:5)在Route.dispatch的下一个(c:\ nodejs \ node_modules \ express \ lib \ router \ route.js:110:13) (c:\ nodejs \ node_modules \ express \ lib \ router \ route.js:91:3)在Layer.handle [as handle_request](c:\ nodejs \ node_modules \ express \ lib \ router \ layer.js:82: 5)在c:\ nodejs \ node_modules \ express \ lib \ router \ index.js:267:22 at Function.proto.process_params(c:\ nodejs \ node_modules \ express \ lib \ router \ index.js:321:12 )下一步(c:\ nodejs \ node_modules \ express \ lib \ router \ index.js:261:10)

/server.就是

var express = require('express');
//route
var routes = require('./routes');
var path = require('path');

var app = express();
var bodyParser = require('body-parser');

app.use(express.static(__dirname + "/public"));
app.set('views', path.join(__dirname, 'views'));

app.set('view engine', 'vash');

app.get('/', routes.index);
app.get('/about', routes.about);
app.get('/contact', routes.contact);
app.use(bodyParser.json()); 
app.listen(3000);
console.log("yep >>> Server running on port 3000");

/routes/index.就是

exports.index = function (req, res) {
    res.render('index', { title: 'Express', year: new Date().getFullYear() });
};

exports.about = function (req, res) {
    res.render('about', { title: 'About', year: new Date().getFullYear(), message: 'Your application description page.' });
};

exports.contact = function (req, res) {
    res.render('contact', { title: 'Contact', year: new Date().getFullYear(), message: 'Your contact page.' });
};

这是文件结构:

node.js express
3个回答
0
投票

由于您没有为视图使用.vash文件扩展名,因此您需要告诉Express查找.html以及如何通过添加以下内容来处理它们:

app.engine('html', require('vash').renderFile);

0
投票

我觉得你对这里的两件事感到困惑:

  1. 如何表达服务模板
  2. 路由如何工作

就服务模板而言,最好的资源是阅读有关res.render(..)的快速官方文档

如果您希望vash成为您的默认模板引擎,那么您应该提到:

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

接下来是关于在路由上提供.vash文件。

在此之前,让我先纠正你的路线。如果我正确理解您要实现的以下路由:

  1. http://localhost:3000/
  2. http://localhost:3000/about
  3. http://localhost:3000/contact

为此,您需要在server.js中添加以下行:

var indexRoutes = require('./routes/index')
app.use('/', indexRoutes);

您可以将./routes/index.js文件更改为:

    var router = express.Router();
    /* GET home page. */
    router.get('/', function(req, res) {
      res.render('indexTpl', {title: 'Express', year: new Date().getFullYear()},            
         function(err, html) {
           // ...
         });
    });
    router.get('/about', function(req, res) {
      res.render('aboutTpl', { title: 'About', year: new Date().getFullYear(), message: 'Your application description page.' },            
         function(err, html) {
           // ...
         });
    });
router.get('/', function(req, res) {
      res.render('contactTpl',{ title: 'Contact', year: new Date().getFullYear(), message: 'Your contact page.' },            
         function(err, html) {
           // ...
         });
    });
    module.exports = router;

请注意我将模板的名称更改为indexTpl.vish,contactTpl.vish,aboutTpl.vish。在运行程序之前,请确保在“views”文件夹中包含这些.vish文件。

注意:您可以在server.js中注释下面提到的三行,因为我们在index.js文件中使用路由器:

app.get('/', routes.index);
app.get('/about', routes.about);
app.get('/contact', routes.contact);

有关更多理解,请参阅this示例。


0
投票

您的文件扩展名错误。让我们改变

index.vash

代替

index.html
© www.soinside.com 2019 - 2024. All rights reserved.