Reputation: 19
I am using node.js and Jade, and I have index.jade in my public/views folder, however it's not showing up when I run the server and go to the page, even though everything is OK, there are no errors, and it should render. This is the code in the 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"));
});
And this is my 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 should run properly, when I go to xxx.xxx.xxx:8080, it just shows "Server is up and running", but isn't showing the index.jade. Any help is appreciated.
Upvotes: 1
Views: 1391
Reputation: 5908
As I see it, you have forgotten to include the router
middleware. Also, in your route handler you are returning two responses. If you simply want to render the index view, remove the 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"));
});
Also, always keep in mind this very important fact about express: the order in which you define your middleware is critical! That is why I moved the static
middleware after router
in order to give priority to router
, since this is the middleware that handles the route callbacks which you define in your application.
Upvotes: 2