node.js - 在 node/express 中找不到 View

标签 node.js express

正如我的用户名所暗示的,我是 node.js 的新手。我正在努力学习它。作为这个过程的一部分,我正在努力建立一个基本的网站。该网站将显示几个基本网页并公开一个 REST 端点。我的项目结构是:

config.js
home.html
start.js
routes.js
server.js
resources
  css
    style.css
  images
    up.png
    down.png
  javascript
    home.html.js

start.js 有我的主要服务器代码。该文件使用“node start.js”通过命令行执行。启动后,我的服务器开始监听端口 3000。start.js 中的代码如下所示:

var express = require('express');
var app = express();

var UserProfileHandler = require('./app/handlers/UserProfileHandler');

app.configure(function () {
  app.engine('html', require('ejs').renderFile);

  app.set('views', __dirname + '/');

  app.use(express.logger({ stream: expressLogFile }));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

var routes = {
    userProfiles: new UserProfileHandler()
};

function start() {
    routeConfig.setup(app, routes);
    var port = process.env.PORT || 3000;
    app.listen(port);
    console.log("SUCCESS: Server listening on port %d in %s mode", port, app.settings.env);
}

exports.start = start;
exports.app = app;

我的 routes.js 文件包含以下内容:

function setup(app, routes) {
  viewSetup(app);
  apiSetup(app, routes);
}

function viewSetup(app) {
  app.get('/', function (req, res) {
    res.render("/home.html");
  });

  app.get('/home.html', function (req, res) {
    res.render("/home.html");
  });
}

function apiSetup(app, routes) {
  app.get('/api/userProfiles/:username', routes.userProfiles.getUserProfiles);
}

我正在尝试在浏览器窗口中加载 home.html。我尝试访问 http://localhost:3000http://localhost:3000/http://localhost:3000/home.html 。不幸的是,这些都不起作用。事实上,我收到一条错误消息:

Express 500 错误:查找 View “/home.html”失败

我知道我很接近。如果我访问 http://localhost:3000/api/userProfiles/me,我会像预期的那样收到 JSON 响应。出于某种原因,我似乎无法返回 HTML。我的 home.html 文件如下所示。

<html>
  <head>
    <script type='text/javascript' src='/resources/javascript/home.html.js'></script>
  </head>
  <body>
    We're up and running! <img src='/resources/images/up.png' />
  </body>
</html>

这是一个非常基本的 HTML 文件。即使 HTML 返回,我也担心它引用的 JavaScript 文件和图像将无法访问。我很担心这一点,因为我不太确定路径等在 Node 中是如何工作的。

如何让 home.html 在我的 Node 设置中工作?

谢谢!

最佳答案

因为你的 View 文件和你的主文件在同一个文件夹中,下面的更改应该可以让它工作

1.更改 View 文件夹配置行

来自

app.set('views', __dirname + '/');//wont work

app.set('views', __dirname);//will work

2.更改 View 渲染线

来自

res.render("/home.html");//wont work

res.render("home.html");//will work

通过这两项更改, View 应该可以正常工作

更新以下评论。

您提到的关于图像、css 和 js 的问题是由于静态文件夹配置导致的,应该从

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

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

因为您的静态文件夹名为 resources

但请确保在您的 View 中您引用的是 css/js/图像文件,例如

例如:

/css/style.css 
/images/up.png
/images/down.png
/javascript/home.html.js

来自你的 View 文件

此外,如果上述方法有效,请检查您是否正确提供了路径,您也可以尝试使用

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

之前

app.use(express.methodOverride());
app.use(app.router); 

像这样的线条

app.configure(function () {
  app.engine('html', require('ejs').renderFile);
  app.set('views', __dirname + '/');
  app.use(express.static(__dirname + '/public'));
  //try changing the position of above line in app.configure and resatrt node app
  app.use(express.logger({ stream: expressLogFile }));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);          
});

关于node.js - 在 node/express 中找不到 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18409374/

相关文章:

node.js - child_process spawn() 中的通配符?

javascript - 如何使用 JavaScript AWS IAM SDK 3 处理限制?

node.js - 如何保存数组?

javascript - Firefox 移动设备上的相机访问问题

javascript - Node.JS:如何等待进程完成后再继续?

node.js - 在发送 POST 之前与远程服务器协商最大请求体大小

javascript - 使用 NodeJS 编写实时应用程序

node.js - 在 Node Express 服务器上测试远程 IP 地址行为

javascript - 平均堆栈 Facebook 授权错误 : No 'Access-Control-Allow-Origin' header?

node.js - 对于任何字符串类型错误显示相同的自定义错误消息 [ EXPRESS + JOI ]