"fs未定义".如何在VueJS文件中使用fs [英] "fs is not defined" How can I use fs within a VueJS file

查看:794
本文介绍了"fs未定义".如何在VueJS文件中使用fs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在在线研究,许多其他人遇到了类似的问题,这些问题尚未解决.

I have been researching online and many others have ran in to similar problems that were left unresolved.

为什么我不能在VueJS文件中使用fs,我读到它不能在客户端调用,但是在文档中使用了它,所以我在做什么错了?

Why is that I can't use fs in VueJS file, I have read that it cannot be called on the client side however it is used within the docs so what am I doing wrong?

https://socket.io/docs/client-api/#With-self-signed-certificate

socket.js文件:

socket.js file:


const fs = require('fs');

var https = require('https').createServer(app, {
  key: fs.readFileSync('server-key.pem'),
  cert: fs.readFileSync('server-cert.pem'),
  passphrase: ''
});

chat.js文件:

chat.js file:

import io from 'socket.io-client';

const socket = io('my_public_ip:port', {
  ca: fs.readFileSync('server-cert.pem'),  //unable to read fs here
});

推荐答案

在我看来,这相对于客户端"的概念是一种误解:

It seems to me that this is rather a misconception regarding the concept of "client":

  • 客户端"以套接字的形式可以是能够连接到套接字服务器的任何软件(例如:服务器,浏览器,移动应用,游戏...)

  • "Client", in socket terms, can be any software able to connect to the socket server (eg.: servers, browsers, mobile apps, games...)

服务器",以套接字的形式,是负责接受连接并收集客户端的代理.

"Server", in socket terms, is the agent responsible for accepting connections, gathering the clients.

根据模块(fs)本身,它在浏览器环境中不起作用,因为它依赖于Node.js内核,该内核捆绑在Node.js安装软件包中,并且仅在Node.js服务器上可用.

As per the module (fs) itself, it does not work on the browser environment because it depends on the Node.js core, which comes bundled on the Node.js installation packages and is only available on Node.js servers.

在浏览器端,为了连接到安全(https)套接字服务器,您需要做的就是在连接时设置 secure 参数,如下所示:

On the browser side, in order to connect to secure (https) socket servers, all you need to do is set the secure parameter while connecting, as in:

const socket = io.connect('my_public_ip:port', { secure: true });

这篇关于"fs未定义".如何在VueJS文件中使用fs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆