【问题标题】:Node.js simple server having issues with embedded javascript in html file (routes.js)Node.js 简单服务器在 html 文件 (routes.js) 中存在嵌入式 javascript 问题
【发布时间】:2015-06-29 06:25:26
【问题描述】:

所以我正在尝试编写一个简单的节点服务器,当它在根目录中受到 get 请求时,它将获取一个非常简单的 html 文件。一切正常,直到我尝试将 javascript 文件附加到我的 html 文件,然后我遇到错误 Uncaught SyntaxError: Unexpected token < 我已经意识到这只是一个 404 错误,但我完全无法弄清楚如何让它停止正在发生。

test.html

<!DOCTYPE html>
<html>
<head>
	<title>title</title>
</head>
<body>
<script type = "text/javascript" src = "/home/danny/Documents/Application/server/test.js"> </script>
booya
</body>
</html>

app.js

/**
 * Main application file
 */

'use strict';

var express = require('express');
var mongoose = require('mongoose');
var tweet = require('../models/tweet.js');

// Connect to database
mongoose.connect('mongodb://localhost/test');

// Setup server
var express = require('express');
var app = express();
require('./config/express');
require('./routes')(app);

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

var server = require('http').createServer(app);

// Start server
server.listen(3000, function() {

  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);

});

console.log(__filename);

// Expose app
exports = module.exports = app;

routes.js

/**
 * Main application routes
 */

'use strict';

module.exports = function(app) {

  // Insert routes below
  app.route('/aa')
    .get(function(req, res) {
      res.sendFile('/home/danny/Documents/Application/server/test.js')
    })

  // All other routes should redirect to the index.html
  app.route('/*')
    .get(function(req, res) {
      // res.set('Content-Type', 'text/html');
      res.render('/home/danny/Documents/Application/src/test.html');
    });
};

有人知道为什么找不到文件吗?

【问题讨论】:

  • 我认为问题在于您的路径,而不是利用 Express 的静态文件服务。看看这个 SO 帖子:stackoverflow.com/questions/10434001/…
  • 好的,谢谢,我会努力解决这个问题。非常感谢!

标签: javascript html node.js routes


【解决方案1】:

我将在这里进行长时间的猜测,但也许是因为您正在这样做:/home/danny/Documents/&lt;xyz&gt;...?

这不起作用,因为您正在尝试链接到 http://yourdomain.com/home/danny/Documents/&lt;xyz&gt;

尝试通过放置 Web 服务器可以理解的路径来解决此问题。

【讨论】:

    猜你喜欢
    • 2015-03-11
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    相关资源
    最近更新 更多