表达抛出意外的标记“缩进”

发布于 2024-12-27 11:55:46 字数 3255 浏览 4 评论 0原文

我正在尝试提供一个使用 jquery 和其他库的静态 html 文件 我想知道如何用快递来做到这一点。

这是我的 app.js 代码(快速服务器文件)

var express = require('express') 
 , routes = require('./routes') 
var app = module.exports = express.createServer(); 
app.configure(function(){ 
 app.set('views', __dirname + '/views'); 
 app.use(express.static(__dirname + '/public')); 
}); 

app.configure('development', function(){ 
 app.use(express.errorHandler({ dumpExceptions: true, showStack: 
true })); 
}); 

app.configure('production', function(){ 
 app.use(express.errorHandler()); 
}); 

app.register('.html', require('jade')); 
app.get('/', function(req, res) { 
   res.render('index.html'); 
}); 

app.listen(3000); 

和我的 index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http:// 
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 
<head> 
<link rel='stylesheet' type='text/css' href='theme.css' /> 
<link rel='stylesheet' type='text/css' href='fullcalendar.css' /> 
<link rel='stylesheet' type='text/css' href='fullcalendar.print.css' 
media='print' /> 
<script type='text/javascript' src='jquery-ui-1.8.11.custom.min.js'></ 
script> 
<script type='text/javascript' src='jquery-1.5.2.min.js'></script> 
<script type='text/javascript' src='fullcalendar.min.js'></script> 
<script type='text/javascript'> 
       $(document).ready(function() { 
               $('#calendar').fullCalendar({ 
                       theme: true, 
                       header: { 
                               left: 'prev,next today', 
                               center: 'title', 
                               right: 'month' 
                       }, 
                       editable: true, 
                       events: { url : 'http://localhost:5555/'}, 
                       firstDay : 1, 
                       weekends : true 
               }); 
       }); 
</script> 
<style type='text/css'> 
       body { 
               margin-top: 40px; 
               text-align: center; 
               font-size: 13px; 
               font-family: "Lucida 
Grande",Helvetica,Arial,Verdana,sans-serif; 
               } 
       #calendar { 
               width: 900px; 
               margin: 0 auto; 
               } 
</style> 
</head> 
<body> 
<div id='calendar'></div> 
</body> 
</html> 

,当我运行我的服务器并将浏览器指向它时,我得到了以下错误,

Error: D:\Workspace\nodejs\test\my-server/views/index.html:12 
   10| <script type='text/javascript'> 
   11| 
 > 12|         $(document).ready(function() { 
   13| 
   14|                 $('#calendar').fullCalendar({ 
   15|                         theme: true, 
unexpected token "indent" 
   at Object.parseExpr (D:\Workspace\nodejs\test\my-server 
\node_modules\jade\lib\parser.js:228:15) 
   at Object.parse (D:\Workspace\nodejs\test\my-server\node_modules 
\jade\lib\parser.js:129:25) 
   at parse (D:\Workspace\nodejs\test\my-server\node_modules\jade\lib 
\jade.js:101:62) 
   at Object.compile (D:\Workspace\nodejs\test\my-server\node_modules 
\jade\lib\jade.js:148:9) 

我进行了初始谷歌搜索,但无法计算出太多.. 有人可以告诉我发生了什么事吗? 谢谢

I'm trying to deliver a static html file which uses jquery and other libraries
and i want to know how to do his with express.

Here's my app.js code ( express server file )

var express = require('express') 
 , routes = require('./routes') 
var app = module.exports = express.createServer(); 
app.configure(function(){ 
 app.set('views', __dirname + '/views'); 
 app.use(express.static(__dirname + '/public')); 
}); 

app.configure('development', function(){ 
 app.use(express.errorHandler({ dumpExceptions: true, showStack: 
true })); 
}); 

app.configure('production', function(){ 
 app.use(express.errorHandler()); 
}); 

app.register('.html', require('jade')); 
app.get('/', function(req, res) { 
   res.render('index.html'); 
}); 

app.listen(3000); 

and my index.html is

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http:// 
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 
<head> 
<link rel='stylesheet' type='text/css' href='theme.css' /> 
<link rel='stylesheet' type='text/css' href='fullcalendar.css' /> 
<link rel='stylesheet' type='text/css' href='fullcalendar.print.css' 
media='print' /> 
<script type='text/javascript' src='jquery-ui-1.8.11.custom.min.js'></ 
script> 
<script type='text/javascript' src='jquery-1.5.2.min.js'></script> 
<script type='text/javascript' src='fullcalendar.min.js'></script> 
<script type='text/javascript'> 
       $(document).ready(function() { 
               $('#calendar').fullCalendar({ 
                       theme: true, 
                       header: { 
                               left: 'prev,next today', 
                               center: 'title', 
                               right: 'month' 
                       }, 
                       editable: true, 
                       events: { url : 'http://localhost:5555/'}, 
                       firstDay : 1, 
                       weekends : true 
               }); 
       }); 
</script> 
<style type='text/css'> 
       body { 
               margin-top: 40px; 
               text-align: center; 
               font-size: 13px; 
               font-family: "Lucida 
Grande",Helvetica,Arial,Verdana,sans-serif; 
               } 
       #calendar { 
               width: 900px; 
               margin: 0 auto; 
               } 
</style> 
</head> 
<body> 
<div id='calendar'></div> 
</body> 
</html> 

and when i run my server and point my browser to it, i get foll error

Error: D:\Workspace\nodejs\test\my-server/views/index.html:12 
   10| <script type='text/javascript'> 
   11| 
 > 12|         $(document).ready(function() { 
   13| 
   14|                 $('#calendar').fullCalendar({ 
   15|                         theme: true, 
unexpected token "indent" 
   at Object.parseExpr (D:\Workspace\nodejs\test\my-server 
\node_modules\jade\lib\parser.js:228:15) 
   at Object.parse (D:\Workspace\nodejs\test\my-server\node_modules 
\jade\lib\parser.js:129:25) 
   at parse (D:\Workspace\nodejs\test\my-server\node_modules\jade\lib 
\jade.js:101:62) 
   at Object.compile (D:\Workspace\nodejs\test\my-server\node_modules 
\jade\lib\jade.js:148:9) 

I did initial googling, but coudn't figure much..
can somone let know wht's happening.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谷夏 2025-01-03 11:55:47

res.render() 会将“index.html”视为模板,并尝试通过模板引擎(jade)处理它。因为您的 HTML 文件不是格式化模板(它只是一个静态 HTML 文件),您会收到错误。

如果您只想提供静态文件index.html,只需将其放在app.use(express.static(__dirname + '/public'));中声明的静态目录中并直接访问即可。静态目录中的文件未经过预处理。如果您尝试执行更复杂的操作,您可能还需要配置 Express 中间件。查看中间件部分的末尾 http://expressjs.com/guide.html#middleware

您还可以使用 fs 模块读取文件内容并提供服务(这看起来就是您当前正在尝试做的事情),但这会带来不必要的开销:

//var fs = require('fs');
app.get('/html', function(req, res) { 
  res.writeHead(200, {'Content-Type': 'text/html'});
  var contents = fs.readFileSync("./public/index.html", "UTF-8");
  res.end(contents);
}); 

res.render() is going to treat "index.html" as a template and try to process it through the templating engine (jade.) Since your HTML file is not a formatted template (it's just a static HTML file), you're getting errors.

If you want to simply serve the static file index.html, just put it in the static directory declared in app.use(express.static(__dirname + '/public')); and access it directly. Files in the static directory are not preprocessed. You may also need to configure the Express middleware if you're trying to do something more complex. Check out the end of the Middleware section at http://expressjs.com/guide.html#middleware

You could also read the file contents using the fs module and serve (which looks like is what you're currently trying to do), but that introduces unnecessary overhead:

//var fs = require('fs');
app.get('/html', function(req, res) { 
  res.writeHead(200, {'Content-Type': 'text/html'});
  var contents = fs.readFileSync("./public/index.html", "UTF-8");
  res.end(contents);
}); 
抱着落日 2025-01-03 11:55:46

问题出在.html与jade模板引擎的连接上。 Jade 要求“html”文件如下所示:

!!! 5
html(lang="en")
  head
    title= pageTitle
    script(type='text/javascript')
      if (foo) {
         bar()
      }
  body
    h1 Jade - node template engine
    #container
      - if (youAreUsingJade)
        p You are amazing
      - else
        p Get on it!

在您的情况下,Jade 引擎尝试将您的 HTML 文件解析为 Jade 模板并阻塞。如果您确实想提供纯 HTML 文件,您可以查看此答案

The problem lies in the connection of .html with the jade template engine. Jade requires the 'html' file to look like this:

!!! 5
html(lang="en")
  head
    title= pageTitle
    script(type='text/javascript')
      if (foo) {
         bar()
      }
  body
    h1 Jade - node template engine
    #container
      - if (youAreUsingJade)
        p You are amazing
      - else
        p Get on it!

and in your case the jade engine tries to parse your HTML file as a jade template and chokes. If you really want to server a pure HTML file, you can take a look at this answer.

z祗昰~ 2025-01-03 11:55:46

只适合任何正在经历这个过程的人。
您很可能正在遵循此处的示例。

https://github.com/visionmedia/express/blob/master/examples/ejs/index.js

如果您没有并且仍然对提供 html 文件感兴趣,请进行活动。那么您需要做的就是使用 ejs 。

如果你的项目没有使用jade模板,那么只需将你的package.json文件从这个:编辑

"jade": "*"

到这个

"ejs": "*"

,然后你就可以使用以下app.js配置:

app.engine('.html', require('ejs').__express);

app.set('view engine', 'html');

当然顺序很重要。

just for anyone who is going though this .
You are most probably following the example over here .

https://github.com/visionmedia/express/blob/master/examples/ejs/index.js

and event if your not and still interested in serving html files . then all you need to do is use ejs .

if your project is not using jade templates then just edit you package.json file from this :

"jade": "*"

to this

"ejs": "*"

and then your all good to go with the following app.js configuration :

app.engine('.html', require('ejs').__express);

app.set('view engine', 'html');

sure thing order is important .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文