node.js + express + jade:style.css内部服务器错误500

时间:2014-01-08 10:08:19

标签: javascript css node.js express pug

我有一个node.js + express + jade的问题,我不知道如何解决,我已经搜索过谷歌和stackoverflow没有解决方案,所以我请你帮我请:)) p>

问题是除了style.css之外的所有工作:

Failed to load resource: the server responded with a status of 500 (Internal 
 Server Error)

P.S:我已经重新安装了最新版本的 node.js 和安全模块,没有解决方案

更新:仅在我在jade文件中添加link(rel='stylesheet', href='style.css')时出现错误

app.js

var express = require("express"); 
app = express(),
routes = require('./routes');

app.configure(function(){
    app.set('port', 8080);
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.urlencoded());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static(__dirname + '/public'));
});

app.get("/", routes.index);

app.listen(app.get("port"), function() {
    console.log("App listening on port " + app.get("port"));
});

路由/ index.js

exports.index = function(req, res) {
    res.render('index');
};

公开/ style.css中

body {
    font-size: 20px;
}

我收到此错误:

my_path_privacy\nodejs-test>node app.js
    App listening on port 8080
    ←[90mGET / ←[32m200 ←[90m3674ms - 867b←[0m
    TypeError: Object #<SendStream> has no method 'on'
        at Object.staticMiddleware [as handle] (my_path_privacy\nodejs-test\node_modules\express\node_modules\connect\lib\middleware\static.js:89:8)
        at next (my_path_privacy\nodejs-test\node_modules\express\node_modules\connect\lib\proto.js:193:15)
        at pass (my_path_privacy\nodejs-test\node_modules\express\lib\router\index.js:110:24)
        at Router._dispatch (my_path_privacy\nodejs-test\node_modules\express\lib\router\index.js:173:4)
        at Object.router [as handle] (my_path_privacy\nodejs-test\node_modules\express\lib\router\index.js:33:10)
        at next (my_path_privacy\nodejs-test\node_modules\express\node_modules\connect\lib\proto.js:193:15)
        at Object.methodOverride [as handle] (my_path_privacy\nodejs-test\node_modules\express\node_modules\connect\lib\middleware\methodOverride.js:48:5)
        at next (my_path_privacy\nodejs-test\node_modules\express\node_modules\connect\lib\proto.js:193:15)
        at Object.urlencoded [as handle] (my_path_privacy\nodejs-test\node_modules\express\node_modules\connect\lib\middleware\urlencoded.js:41:37)
        at next (my_path_privacy\nodejs-test\node_modules\express\node_modules\connect\lib\proto.js:193:15)
    ←[90mGET /style.css ←[31m500 ←[90m52ms - 1.34kb←[0m

的package.json

{
   "name": "app-test",
   "version": "0.0.1",
   "private": true,
   "scripts": {
       "start": "node app.js"
   },
   "dependencies": {
      "express": "3.4.7",
      "jade": "1.1.0"
   },
   "engines": {
      "node": "0.10.24",
      "npm": "1.3.21"
  }
}

index.jade

doctype html
html
  head
    title Hello world
    link(rel='stylesheet', href='style.css')
  body
    p Hello world

2 个答案:

答案 0 :(得分:0)

我不知道它是否相关,我在jade文件中嵌入脚本有很多麻烦。语法已更改。如果在下一行中遵循JS,​​则可能必须在jade文件中的脚本标记中添加一个点。

还有另一个邪恶的变化,你需要写doctype html而不是doctype 5

script
  var x=1

is now

script.
  var x=1

答案 1 :(得分:0)

看起来像一个模板引擎问题,只是出于好奇,你可以尝试发送数组或对象而不是“Hello,World”,只需替换

res.render('index', {title: "Hello, world"});

res.render('index', {title: some array or object here});