node.js中的beetwen path.join(__ dirname,' views')和path.join(__ dirname +' views')之间存在差异

时间:2017-11-15 10:00:14

标签: javascript node.js join path

我再次坚持使用node.js.

我在设置视图引擎的代码中尝试第一个path.join它无法正常工作,索引不会显示,然后我更改为| ____ _____ _ ____ _____ | / ___| |_ _| / \ | _ \ |_ _| | \___ \ | | / _ \ | |_) | | | | ___) | | | / ___ \ | _ < | | | |____/ |_| /_/ \_\ |_| \_\ |_| | | Starting the network | | Channel name : mychannel | Creating channel... | CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt | CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key | CORE_PEER_LOCALMSPID=Org1MSP | CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock | CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt | CORE_PEER_TLS_ENABLED=false | CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp | CORE_PEER_ID=cli | CORE_LOGGING_LEVEL=DEBUG | CORE_PEER_ADDRESS=peer0.org1.example.com:7051 | 2017-11-15 09:31:36.011 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP | 2017-11-15 09:31:36.012 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity | 2017-11-15 09:31:36.017 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized | 2017-11-15 09:31:36.018 UTC [msp] GetLocalMSP -> DEBU 004 Returning existing local MSP | 2017-11-15 09:31:36.019 UTC [msp] GetDefaultSigningIdentity -> DEBU 005 Obtaining default signing identity | 2017-11-15 09:31:36.019 UTC [msp] GetLocalMSP -> DEBU 006 Returning existing local MSP | 2017-11-15 09:31:36.019 UTC [msp] GetDefaultSigningIdentity -> DEBU 007 Obtaining default signing identity | 2017-11-15 09:31:36.019 UTC [msp/identity] Sign -> DEBU 008 Sign: plaintext: 0AC6060A074F7267314D535012BA062D...53616D706C65436F6E736F727469756D | 2017-11-15 09:31:36.019 UTC [msp/identity] Sign -> DEBU 009 Sign: digest: D6E8392380793B24537309F14EA1C0D9CF3F18FF8292A65D09CF3AA92EA2094D | 2017-11-15 09:31:36.019 UTC [msp] GetLocalMSP -> DEBU 00a Returning existing local MSP | 2017-11-15 09:31:36.019 UTC [msp] GetDefaultSigningIdentity -> DEBU 00b Obtaining default signing identity | 2017-11-15 09:31:36.019 UTC [msp] GetLocalMSP -> DEBU 00c Returning existing local MSP | 2017-11-15 09:31:36.019 UTC [msp] GetDefaultSigningIdentity -> DEBU 00d Obtaining default signing identity | 2017-11-15 09:31:36.019 UTC [msp/identity] Sign -> DEBU 00e Sign: plaintext: 0AFD060A1508021A0608F892B0D00522...628AB20AD0563C9EA2A482A301EA32D8 | 2017-11-15 09:31:36.019 UTC [msp/identity] Sign -> DEBU 00f Sign: digest: 5A58E17C75478098A108CFCFA1E909639C7830022602D225A61C4D0BE9E8C5AD | 2017-11-15 09:31:36.060 UTC [msp] GetLocalMSP -> DEBU 010 Returning existing local MSP | 2017-11-15 09:31:36.060 UTC [msp] GetDefaultSigningIdentity -> DEBU 011 Obtaining default signing identity | 2017-11-15 09:31:36.060 UTC [msp] GetLocalMSP -> DEBU 012 Returning existing local MSP | 2017-11-15 09:31:36.060 UTC [msp] GetDefaultSigningIdentity -> DEBU 013 Obtaining default signing identity | 2017-11-15 09:31:36.060 UTC [msp/identity] Sign -> DEBU 014 Sign: plaintext: 0AFD060A1508021A0608F892B0D00522...5B18D1838ED112080A021A0012021A00 | 2017-11-15 09:31:36.060 UTC [msp/identity] Sign -> DEBU 015 Sign: digest: 4309C46AA7BBA47AD146AA77CB1ABAC79114C9C3D66D41B833F82ED7F882E326 | 2017-11-15 09:31:36.082 UTC [channelCmd] readBlock -> DEBU 016 Received block: 0 | 2017-11-15 09:31:36.083 UTC [main] main -> INFO 017 Exiting..... | ===================== Channel "mychannel" is created successfully ===================== | | Having all peers join the channel... | CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt | CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key | CORE_PEER_LOCALMSPID=Org1MSP | CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock | CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt | CORE_PEER_TLS_ENABLED=false | CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp | CORE_PEER_ID=cli | CORE_LOGGING_LEVEL=DEBUG | CORE_PEER_ADDRESS=peer0.org1.example.com:7051 | 2017-11-15 09:31:36.121 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP | 2017-11-15 09:31:36.121 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity | 2017-11-15 09:31:36.123 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized | 2017-11-15 09:31:36.123 UTC [msp/identity] Sign -> DEBU 004 Sign: plaintext: 0AC3070A5B08011A0B08F892B0D00510...A1A603DD33A31A080A000A000A000A00 | 2017-11-15 09:31:36.123 UTC [msp/identity] Sign -> DEBU 005 Sign: digest: 5D870839DD3A368A48E2CED8314E3817CA48001BACBF74B36380408D851769AE | 2017-11-15 09:31:36.158 UTC [channelCmd] executeJoin -> INFO 006 Peer joined the channel! | 2017-11-15 09:31:36.158 UTC [main] main -> INFO 007 Exiting..... | ===================== PEER0 joined on the channel "mychannel" ===================== | sleep: missing operand | Try 'sleep --help' for more information. | | CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt | CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key | CORE_PEER_LOCALMSPID=Org1MSP | CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock | CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt | CORE_PEER_TLS_ENABLED=false | CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp | CORE_PEER_ID=cli | CORE_LOGGING_LEVEL=DEBUG | CORE_PEER_ADDRESS=peer1.org1.example.com:7051 | 2017-11-15 09:31:36.194 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP | 2017-11-15 09:31:36.194 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity | 2017-11-15 09:31:36.196 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized | 2017-11-15 09:31:36.196 UTC [msp/identity] Sign -> DEBU 004 Sign: plaintext: 0AC3070A5B08011A0B08F892B0D00510...A1A603DD33A31A080A000A000A000A00 | 2017-11-15 09:31:36.196 UTC [msp/identity] Sign -> DEBU 005 Sign: digest: 0B0C3F308D78AC2FDD6FC89A71FED2DCB889038AE993980A6E4B540BB4D3C51A | 2017-11-15 09:31:36.248 UTC [channelCmd] executeJoin -> INFO 006 Peer joined the channel! | 2017-11-15 09:31:36.248 UTC [main] main -> INFO 007 Exiting..... | ===================== PEER1 joined on the channel "mychannel" ===================== | sleep: missing operand | Try 'sleep --help' for more information. | | CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt | CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key | CORE_PEER_LOCALMSPID=Org2MSP | CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock | CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt | CORE_PEER_TLS_ENABLED=false | CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp | CORE_PEER_ID=cli | CORE_LOGGING_LEVEL=DEBUG | CORE_PEER_ADDRESS=peer0.org2.example.com:7051 | 2017-11-15 09:31:36.288 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP | 2017-11-15 09:31:36.288 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity | 2017-11-15 09:31:36.289 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized | 2017-11-15 09:31:36.290 UTC [msp/identity] Sign -> DEBU 004 Sign: plaintext: 0AC4070A5C08011A0C08F892B0D00510...A1A603DD33A31A080A000A000A000A00 | 2017-11-15 09:31:36.290 UTC [msp/identity] Sign -> DEBU 005 Sign: digest: 992F4939F777DD575F2753ADF6936A7E6FB9CC8548C188B31E25B06F9ECEA7E7 | 2017-11-15 09:31:36.335 UTC [channelCmd] executeJoin -> INFO 006 Peer joined the channel! | 2017-11-15 09:31:36.335 UTC [main] main -> INFO 007 Exiting..... | ===================== PEER2 joined on the channel "mychannel" ===================== | sleep: missing operand | Try 'sleep --help' for more information. | | CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt | CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key | CORE_PEER_LOCALMSPID=Org2MSP | CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock | CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt | CORE_PEER_TLS_ENABLED=false | CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp | CORE_PEER_ID=cli | CORE_LOGGING_LEVEL=DEBUG | CORE_PEER_ADDRESS=peer1.org2.example.com:7051 | 2017-11-15 09:31:36.372 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP | 2017-11-15 09:31:36.372 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity | 2017-11-15 09:31:36.373 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized | 2017-11-15 09:31:36.374 UTC [msp/identity] Sign -> DEBU 004 Sign: plaintext: 0AC4070A5C08011A0C08F892B0D00510...A1A603DD33A31A080A000A000A000A00 | 2017-11-15 09:31:36.374 UTC [msp/identity] Sign -> DEBU 005 Sign: digest: 6776D313E8DD88880918868C6BA2C93ECEB05425FE93A7E385762918FA2AF556 | 2017-11-15 09:31:36.419 UTC [channelCmd] executeJoin -> INFO 006 Peer joined the channel! | 2017-11-15 09:31:36.419 UTC [main] main -> INFO 007 Exiting..... | ===================== PEER3 joined on the channel "mychannel" ===================== | sleep: missing operand | Try 'sleep --help' for more information. | | | ========= All GOOD, BYFN execution completed =========== 及其工作好吧,索引显示,我不知道如何

有人可以解释这是如何工作的吗?

path.join(__dirname , 'views');

这是我的剧本

path.join(__dirname + 'views');

path.join(__dirname, 'views');

5 个答案:

答案 0 :(得分:3)

path.join通过将它们与&#34; /&#34;连接起来,从你的参数构造一个路径。 因此path.join(__dirname, 'views')会导致/ path_to_dir / views

另一方面,(__dirname + 'views')只有一个参数(__dirname + views)。这意味着没有其他参数可以加入到第一个参数中,因此您最终得到了&#34; / path_to_dirviews&#34; (没有加入&#34; /&#34;)

答案 1 :(得分:0)

默认函数使用逗号作为分隔符,而不是加号,这就是它使用逗号而不是加号的原因。

请参阅node.js中的path.join()文档

答案 2 :(得分:0)

来自官方文件:  https://nodejs.org/api/path.html#path_path_join_paths

你加入的路径必须用分隔符分隔;

此外,如果所有路径都不是字符串,则会抛出错误。

所以,我认为添加+使路径变得疯狂,因为它找不到分隔符,而且它发现你提到的路径并不都是字符串。

希望这有帮助。

答案 3 :(得分:0)

要知道这个函数的区别,你有official node.JS documentation

使用Javascript连接的第一种方法使用方法参数的结果不同:

使用标准javascript:

let folder = '/folder';
let subFolder = 'sub';

path.join(folder + sub ) // result : /foldersub

但是,通过正确使用此方法的参数,路径是不同的:此方法格式化所有参数,用于创建标准部分 /

let folder = '/folder';
let subFolder = 'sub';

path.join(folder, sub) // result : /folder/sub

不要忘记您可以使用console.log(path)在Javascript ...中进行调试以使用它here

答案 4 :(得分:0)

path.join是一个函数。使用+运算符意味着您只发送一个参数,该参数是__dirname和&#39; views&#39;的串联。 该函数应返回一个由传递的参数构造的路径。在你的情况下只有一个,所以它不会是一个有效的路径。