使用node.js和Jade但index.jade没有显示出来

时间:2013-11-10 14:45:09

标签: node.js express pug

我正在使用node.js和Jade,我的public / views文件夹中有index.jade,但是当我运行服务器并转到页面时它没有出现,即使一切正常,也没有错误,它应该呈现。这是server.js中的代码:

console.log("Hello, world!");

var express = require("express")
  , app = express()
  , http = require("http").createServer(app);

app.set("ipaddr", "192.168.0.103");

app.set("port", 8080);

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

app.set("view engine", "jade");

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

app.use(express.bodyParser());

app.get("/", function(request, response) {

  response.send("Server is up and running");

  response.render("index");

});

http.listen(app.get("port"), app.get("ipaddr"), function() {
  console.log("Server up and running. Go to http://" + app.get("ipaddr") + ":" + app.get("port"));


});

这是我的index.jade:

doctype 5
html
  head
    link(rel='stylesheet', href='http://fonts.googleapis.com/css?family=Open+Sans')
    link(rel='stylesheet', href='/css/style.css')    
    title Super Awesome Chatroom
  body
    h1 Super Awesome Chatroom
    div
      div.inlineBlock
        span Your name:
          input(type="text", value="Anonymous")#name
        br
        form#messageForm
          textarea(rows="4", cols="50", placeholder="Share something!", maxlength=200)#outgoingMessage
          input(type="button", value="Share", disabled=true)#send
      div.inlineBlock.topAligned
        b Participants
        br
        div#participants
    div#messages

Everthing应该正常运行,当我转到xxx.xxx.xxx:8080时,它只显示“服务器已启动并正在运行”,但未显示index.jade。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:2)

正如我所看到的,您忘记了包含router中间件。此外,在您的路由处理程序中,您将返回两个响应。如果您只想渲染索引视图,请删除response.send():

var express = require("express")
  , app = express()
  , http = require("http").createServer(app);

app.set("ipaddr", "192.168.0.103");

app.set("port", 8080);

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

app.set("view engine", "jade");

app.use(express.bodyParser());

app.use(app.router);

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

app.get("/", function(request, response) {

  response.render("index");

});

http.listen(app.get("port"), app.get("ipaddr"), function() {
  console.log("Server up and running. Go to http://" + app.get("ipaddr") + ":" + app.get("port"));


});

另外,请始终牢记关于表达的这一非常重要的事实:您定义中间件的顺序至关重要!这就是为什么我在{{1}之后移动了static中间件为了优先考虑router,因为这是处理你在应用程序中定义的路由回调的中间件。