简体   繁体   English

在Windows上运行但不在Linux上运行的NodeJS Javascript片段代码

[英]NodeJS Javascript piece code running on Windows but not on Linux

I am particularly new to NodeJS and Javascript and have created a small web Application. 我对NodeJS和Javascript特别陌生,并且创建了一个小型Web应用程序。 I have my code running on Windows machine seamlessly. 我的代码在Windows计算机上无缝运行。 However, When I tried running it on a Linux VM it blocked at a point with no error or Exceptions thrown. 但是,当我尝试在Linux VM上运行它时,它阻塞了一点,没有错误或引发异常。

I discovered there was a line which actually caused a blockage in Linux, commented it out and the code continued from there on limiting the Application's functionality which greatly depends on that line with the other independent parts being functional. 我发现有一条线实际上在Linux中造成了阻塞,将其注释掉,然后代码从那里继续限制应用程序的功能,这在很大程度上取决于该行以及其他独立部分的功能。

....
var localEntry = entry.split('\\')
if(!localEntry)
localEntry = entry.split('/')

localEntry = localEntry[localEntry.length -1]

this.scripts[extn][localEntry].day1Vars = searchedVars[entry].day1Vars
}

I was extremely puzzled to find the exact same code running seamlessly on a Windows machine and blocking on a Linux vm due to a single line of code and am wondering how that could be ? 我发现完全相同的代码在Windows计算机上无缝运行,而在Linux vm上由于一行代码而阻塞,我感到非常困惑,我想知道这怎么可能? The line which is responsible for such behavior is : this.scripts[extn][localEntry].day1Vars = searchedVars[entry].day1Vars 导致这种行为的行是: this.scripts[extn][localEntry].day1Vars = searchedVars[entry].day1Vars

My expectation is code on Node.js is platform independent and the thought of a line of code causing such difference is revolting. 我的期望是,Node.js上的代码与平台无关,并且引起这种差异的代码行的想法令人反感。 I am using Windows 10 and RHEL 6.9 with 8GB of ram in both. 我正在同时使用Windows 10和RHEL 6.9和8GB的ram。

Could someone guide me if I am missing something or what has gone wrong? 如果我缺少某些东西或出了什么问题,可以有人指导我吗? Any help is greatly appreciated. 任何帮助是极大的赞赏。

As requested by folks here, Sample values(on Windows) : 根据此人的要求,示例值(在Windows上):

entry : "d:\NodeProjects\BApp\uploads\bp\bp\scripts\nodejs\set-nodejs-root.sh"
localEntry : "set-nodejs-root.sh"

When you specify a path under windows you have to use "\\" 在Windows下指定路径时,必须使用“ \\”
Linux uses "/" for path Linux使用“ /”作为路径

Its better to check what is the environment. 最好检查一下环境。 For example you should split in: 例如,您应该拆分为:

ubuntu .split("/") ubuntu .split("/")

windows .split("\\\\") Windows .split("\\\\")

Looks like you are working with paths, the best recommendation here is to use the path module to handle those routes for you: 看起来您正在使用路径,此处的最佳建议是使用path模块为您处理这些路由:

https://nodejs.org/api/path.html https://nodejs.org/api/path.html

Here is a good explanation on how to handle routes for both systems: 这是有关如何处理两个系统的路由的很好的解释:

https://nodejs.org/api/path.html#path_windows_vs_posix https://nodejs.org/api/path.html#path_windows_vs_posix

I would use something like 我会用类似的东西

https://nodejs.org/api/path.html#path_path_parse_path https://nodejs.org/api/path.html#path_path_parse_path

To parse the paths correctly or if you need to construct paths you can use: 要正确解析路径,或者如果您需要构造路径,可以使用:

https://nodejs.org/api/path.html#path_path_join_paths https://nodejs.org/api/path.html#path_path_join_paths

Check all different options there, I am 99% sure that you will find the proper method for your use case 检查所有不同的选项,我99%确信您会找到适合您的用例的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM