gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:42:52 24 4
gpt4 key购买 nike

我正在使用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

一切都应该正常运行,当我转到xxx.xxx.xxx:8080时,它只显示“服务器已启动并正在运行”,但不显示index.jade。如有任何帮助,我们将不胜感激。

最佳答案

据我所知,您忘记包含 router 中间件。此外,在您的路由处理程序中,您将返回两个响应。如果您只想渲染索引 View ,请删除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"));


});

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

关于node.js - 使用node.js和Jade但index.jade没有显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19891055/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com