[removed] 属性报错,Nodejs求助

[removed] 属性报错,Nodejs求助

刚开始看NODE.JS,有很多问题不明白,特向大家请教~

server监听8888端口,浏览器访问8888端口 server response 页面“index.html" index.html中有一段: <script type ="text/javascript" src="/test.js"> Chrome报错: GET http://localhost:8888/test.js 404 (Not Found)

改为 <script type ="text/javascript" src="http://localhost:8888/test.js"> 依然报错 GET http://localhost:8888/test.js 404 (Not Found)

只得改为用路由response的方法 ``<script type =“text/javascript” src=“http://localhost:8888/gettest”` 同时后台requestHandler新增方法

    function gettest(response, postData) {
    console.log("Request handler 'upload' was called.");
fs.readFile(__dirname+'/test.js',
    function(err,data){
        response.writeHead(200,{"Content-Type": "application/x-javascript"});
        response.write(data);
        response.end();
    }
);

}

代码运行正常了.

请问是我上面的代码有问题吗?<script src>有没有更好的处理办法? 当代码中有很多<script src>时,增加N多requestHandler是一件繁重的事情,还请大家想个能一劳永逸解决的方法。


5 回复

当然可以。根据你的描述,你遇到的是一个静态文件(如 test.js)无法正确加载的问题。通常情况下,这种问题可以通过设置静态文件服务来解决,而不是为每个文件单独编写路由处理程序。

解决方案

使用 Express 框架中的静态文件服务功能是一个更简洁和高效的方法。Express 提供了一个内置的中间件 express.static,它可以自动处理静态文件的请求。

示例代码

首先,你需要安装 Express 框架:

npm install express

然后,在你的服务器代码中,你可以这样设置静态文件服务:

const express = require('express');
const path = require('path');

const app = express();

// 设置静态文件目录
app.use(express.static(path.join(__dirname, 'public')));

// 如果需要响应特定的 HTML 文件
app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

// 启动服务器
app.listen(8888, () => {
    console.log('Server is running on port 8888');
});

静态文件目录结构

假设你的项目目录结构如下:

/project-root
|-- public
|   |-- index.html
|   |-- test.js
|-- server.js

index.html 文件

index.html 中,你可以直接引用 test.js 文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My App</title>
</head>
<body>
    <!-- 直接引用静态文件 -->
    <script type="text/javascript" src="/test.js"></script>
</body>
</html>

解释

  1. 设置静态文件目录:通过 app.use(express.static(path.join(__dirname, 'public'))); 这行代码,Express 将会把 public 目录下的文件作为静态资源进行处理。
  2. 响应 HTML 文件:通过 app.get('/', ...) 来响应根路径的请求,并返回 index.html 文件。
  3. 启动服务器:通过 app.listen(8888, ...) 来启动服务器并监听 8888 端口。

这种方式不仅简化了代码,也提高了可维护性。当你有多个静态文件时,无需为每个文件单独编写路由处理程序。


这是以因为nodejs没有实现像tomcat、nginx那样的静态资源服务,你可以看一下这篇文章:http://www.infoq.com/cn/news/2011/11/tyq-nodejs-static-file-server

建议先实现读取功能就好了,后面的安全策略再慢慢看吧

解决了我遇到的问题,终于明白问题处在哪了,非常感谢!

解决了我遇到的问题,终于明白问题处在哪了,非常感谢!

根据你的描述,你在尝试通过 Node.js 服务器来提供静态文件(如 test.js)给客户端,但遇到了 404 错误。实际上,你可以通过使用中间件来简化这个问题,而不是为每个文件都创建一个特定的处理函数。

解决方案

使用 Express 框架

Express 是一个简洁而灵活的 Node.js Web 应用框架,它可以帮助你更轻松地处理静态文件。

  1. 首先,安装 Express 和 Path(如果你还没有安装 Express 的话):
npm install express path
  1. 然后,你可以设置静态文件服务:
const express = require('express');
const path = require('path');

const app = express();
const port = 8888;

// 设置静态文件目录
app.use(express.static(path.join(__dirname, 'public')));

// 如果需要,也可以设置其他路由
app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, 'index.html'));
});

app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}/`);
});

在这个例子中,我们将 public 目录下的所有文件作为静态文件提供。确保你的 test.js 文件放在 public 目录下。

HTML 示例

确保你的 index.html 中引用脚本的方式是正确的:

<script type="text/javascript" src="/test.js"></script>

这样,无论有多少个 <script> 标签,都可以通过配置一次来处理所有的静态资源请求。这将大大简化你的代码,并使维护变得更加容易。

回到顶部