更新时间:2022-03-15 03:29:28
编辑:重新阅读您的原始文章,您可能实际上正在运行此Node.js.节点环境中的脚本,尝试使用fs
加载.html文件.如果是这样,您就是在.html文件中的 内调用Node server.js脚本,如下所示:
Rereading your original post, it's possible that you are in fact running this Node.js script in a Node environment, trying to use fs
to load your .html file. If that's the case, you are calling your Node server.js script within your .html file like so:
<script type="text/javascript" src="server.js"></script>
只需删除此行,您就不会再收到所看到的错误.
Simply remove this line, and you should no longer receive the error you're seeing.
如前所述,您正在尝试在客户端脚本中实现 Node.js 代码.从理论上讲,可以完成,但是不建议这样做,而Node.js根本不建议这样做为此目的而设计(它是一台服务器!)
As previously stated, you're attempting to implement Node.js code in your client-side script. In theory this can be done, but isn't recommended, and Node.js simply isn't designed for this purpose (it's a server!)
您应该使用 XHR/ajax
从客户端向服务器发出http请求(而不是fs
),然后在后端处理这些事情.可以使用传统的Web堆栈(例如Apache和php)轻松完成此操作,或者如果您确实要使用fs
,请从Node.js运行API,然后使用 Sails .js .
You should be using XHR/ajax
to make http requests from your client to a server (not fs
), and then handle those things on the back end. This can be done easily with a conventional web stack like Apache and php, or if you really want to use fs
, run an API off of Node.js and connect your client to it using XHR/ajax
, or implement an MVC stack like Sails.js.