[英]Absolute path using fs in nodejs
I am trying to pipe an http audio stream
from my nodejs
server: 我试图从我的
nodejs
服务器管道一个http audio stream
:
var streamPath = 'http://127.0.0.1:1485/mystream.mp3';
var stat = fs.statSync(streamPath);
response.writeHead(200, {'Content-Type': 'audio/mpeg','Content-Length': stat.size});
fs.createReadStream(streamPath).pipe(response);
The problem is that fs
doesn't like the absolute path and I get the following error: 问题是
fs
不喜欢绝对路径,我得到以下错误:
Error: ENOENT: no such file or directory, stat 'C:\\myserver\\http:\\127.0.0.1:1485\\mystream.mp3'
错误:ENOENT:没有这样的文件或目录,stat'C:\\ myserver \\ http:\\ 127.0.0.1:1485 \\ mystream.mp3'
I can't find a way to use the absolute path
. 我找不到使用
absolute path
。 Is this even possible using fs
? 这甚至可以用
fs
吗?
'http://127.0.0.1:1485/mystream.mp3'
is not an absolute path, it's a URL. 'http://127.0.0.1:1485/mystream.mp3'
不是绝对路径,而是URL。
Absolute paths are something like /home/x/dir/file.txt
on your filesystem. 绝对路径类似于文件系统上的
/home/x/dir/file.txt
。
If you want to get a stream from a URL then you need to use http
or request
. 如果您想从URL获取流,则需要使用
http
或request
。 Or you need to use a path on your filesystem and not a URL if you want to get a file on your filesystem without using the network. 或者,如果要在不使用网络的情况下获取文件系统上的文件,则需要在文件系统上使用路径而不是URL。
fs.createReadStream
can only open local file in your filesystem. fs.createReadStream
只能在文件系统中打开本地文件。
For more details, see: 有关详细信息,请参阅:
To get a file over a network, see: 要通过网络获取文件,请参阅:
You are trying to access a remote resource through fs
- that is wrong. 您正试图通过
fs
访问远程资源 - 这是错误的。 fs
is meant to be used for the Local Filesystem
. fs
用于Local Filesystem
。 If you want to access any remote resource you have to use http
or https
. 如果要访问任何远程资源,则必须使用
http
或https
。 However if I see things correctly you are trying to access your localhost
, which should work. 但是,如果我正确地看到了您正在尝试访问您的
localhost
,这应该可以正常工作。
Your Application is trying to access following file: C:\\myserver\\http:\\127.0.0.1:1485\\mystream.mp3
if you look closely that can't work. 您的应用程序正在尝试访问以下文件:
C:\\myserver\\http:\\127.0.0.1:1485\\mystream.mp3
如果您仔细查看无法正常工作。 Your Path is mixed with your local path and a remote source (which is localhost
actually). 您的路径与您的本地路径和远程源(实际上是
localhost
)混合在一起。 Try to fix your path, that should solve your problem. 尝试修复你的路径,这应该可以解决你的问题。 Keep in mind that
fs
will only work on your local system. 请记住,
fs
只适用于您的本地系统。
You should also think about fs.statSync
this will block everything else until its finished. 您还应该考虑
fs.statSync
这将阻止其他所有内容,直到它完成。
Docs: 文档:
fs: https://nodejs.org/api/fs.html fs: https : //nodejs.org/api/fs.html
http: https://nodejs.org/api/http.html http: https : //nodejs.org/api/http.html
https: https://nodejs.org/api/https.html https:https: //nodejs.org/api/https.html
Regards, Megajin 此致,Megajin
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.