【问题标题】:Express & Jade - Cannot GET / templateExpress & Jade - 无法获取/模板
【发布时间】:2016-08-12 18:11:32
【问题描述】:

我是 Express 的新手,我正在尝试转换这个项目

https://github.com/shawn-simon-developer/NodeAngularProxyhttps://github.com/shawn-simon-developer/NodeAngularProxy 变成我可以使用 Jade 的地方。 我已经通过 npm 安装了 Jade。我的文件夹结构如下所示:

myProject
--app
----css (inside e.g. bootstrap)
----index.jade
----js
------controllers
------services
----libs
----templates
------main.jade
--index.js
--node_modules
----(tons of modules in here e.g.)
----jade
----sass
----express
--package.json

index.js 看起来像这样:

/**
 * Module dependencies.
 */
var express = require('express');
var jade = require('jade');
var httpProxy = require('http-proxy');
var bodyParser = require('body-parser');

var apiForwardingUrl = 'http://api.open-notify.org/astros.json?';

// Solution for forwarding from http to https taken from:
// http://stackoverflow.com/questions/15801014/how-to-use-node-http-proxy-for-http-to-https-routing
var proxyOptions = {
    changeOrigin: true
};

httpProxy.prototype.onError = function (err) {
    console.log(err);
};

var apiProxy = httpProxy.createProxyServer(proxyOptions);

console.log('Forwarding API requests to ' + apiForwardingUrl);

// Node express server setup.
var server = express();
server.set('port', 3000);
server.use(express.static(__dirname + '/app'));

server.all("/space/*", function(req, res) {
    apiProxy.web(req, res, {target: apiForwardingUrl});
});

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

// Start Server.
server.listen(server.get('port'), function() {
    console.log('Express server listening on port ' + server.get('port'));
});


server.set('view engine', 'jade'); // register the template engine

server.get('/templates/:name', function (req, res)
 { var name = req.params.name;
   res.render('templates/' + name);
});

然后像这样index.jade

html
  head
    title Angular App
    // Angular Modules
    script(type='text/javascript', src='libs/angular/angular.js')
    script(type='text/javascript', src='libs/angular-route/angular-route.js')
    script(type='text/javascript', src='libs/jquery/jquery.js')
    // CSS Modules
    link(rel='stylesheet', type='text/css', href='css/bootstrap/bootstrap.min.css')
  body(ng-app='app')
    .container(ng-view='')
  // JS Modules
  script(type='text/javascript', src='js/app.js')
  script(type='text/javascript', src='js/controllers/MainCtrl.js')
  script(type='text/javascript', src='js/services/apiService.js')

最后是 main.jade

{{ctrl.test}}

p {{ctrl.wikiData | json}}

h1 test

就是这样。我可以在项目的根目录中使用node index.js 启动服务器。但是,当我访问我的localhost:3000 时,我遇到了许多其他人似乎也遇到的Cannot GET / 问题。但是,由于我是 Node 和 Express 的新手,所以我无法概括其他问题的答案并解决我自己的问题。 谁能帮我解决这里出了什么问题? 已经谢谢了!

编辑 1: 我已经添加了

server.set('views', __dirname + '/app/templates');
server.get('/', function (req, res) { res.render('index', {}); }); 
server.get('/', function (req, res) {
     res.send('hello');
});

到 index.js 的末尾,并将 index.jade 文件移动到模板文件夹中。现在我得到的是这样的控制台日志:

angular.js:10506 GET http://localhost:3000/templates/main 404 (Not Found)

这更进一步,但还不是解决方案

【问题讨论】:

    标签: node.js express pug


    【解决方案1】:

    你不应该指向本地主机。而是指向 localhost:3000

    http://localhoat:3000
    

    回答主要问题

    我找不到索引路由器!!你能像这样添加索引路由器吗?

    server.get('/', function (req, res) {
         res.send('hello');
    });
    

    回答第二次编辑

    从参数中移除模板到 res.render 函数。

    server.get('/templates/:name', function (req, res){
        var name = req.params.name;
        res.render( name);
    });
    

    【讨论】:

    • 我当然是在浏览器中打开 localhost:3000 。这不是问题
    • app.get('/', function (req, res) { res.render('index', {}); });
    • 使用您的第二次编辑我回到最初的问题“无法获取 /”
    【解决方案2】:

    尝试将文件结构更改为

    /public [rename app to www, static, pub]
        /css
        /js
        /libs
    /node_modules
    /templates [jade is server-side, so don't share it for browsers]
        /layout
            index.jade
        index.jade
        main.jade
    index.js
    package.json
    

    下面的代码

    var express = require('express');
    var jade = require('jade');
    var server = express();
    ...
    server.use(express.static(__dirname + '/public'));
    server.set('view engine', 'jade'); 
    server.set('views', __dirname + '/templates'); 
    ...
    server.get('/', function (req, res) { 
        res.render('index', {}); 
    });
    
    server.get('/:page', function (req, res) { 
        res.render(req.params.name, {caption: 'Example', text: 'Hello world'}); 
    });
    

    在模板中使用 layout 例如

    // templates/layout/index.jade
    html
      head
        title Angular App
        script(...)
        link(...)
      body(ng-app='app')
        .container(ng-view='')
        block content
    
    // templates/index.jade`
    extends ./layout/index.jade
    block content
      IndexPage
    
    // templates/layout/main.jade`
    extends ./layout/index.jade 
    block content
      h1 {{caption}}
      {{text}}
    

    附: Jade 是服务器端渲染引擎,angular 是客户端。通常其中一个就足够了。

    【讨论】:

    • 尝试收到此错误localhost:3000/templates/main 加载资源失败:服务器响应状态为 404(未找到)
    • 请改用localhost:3000/main。或将server.get('/:page', function (req, res) { 更改为server.get('/templates/:page', function (req, res) {(路由中的/smth 不是非静态文件的路径)。
    • tdid 你的第二个建议。结果“无法获取 /”
    • 如果你不删除代码server.get('/', function (req, res) { ... });,那真是难以置信。附言我不知道你为什么要在路由中使用/templates/...
    • 好的。我当时误解了你的评论。现在我从终端使用路由中的模板得到Error: Failed to lookup view "undefined" in views directory "projecPath/templates" 是你的建议,不是吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 2012-01-17
    相关资源
    最近更新 更多