节点js如何拥有前导路径?

时间:2015-06-26 17:42:32

标签: javascript node.js path cross-platform

我有一条路径可以运行,例如:./abc/def/myshell.sh

我像这样使用path.join

path.join('.', 'abc', 'def', 'myshell.sh');

但它取消了领先期。 我怎么能保留它?还有其他功能吗?

1 个答案:

答案 0 :(得分:0)

根据您是否需要当前工作目录或脚本所在目录,您应使用process.cwd()__dirname作为import React from 'react'; import DocumentTitle from 'react-document-title'; import api from './api'; export default class Login extends React.Component { constructor(props) { super(props); // how to fill this state with POST parameters on error? // how to redirect on success? // and remember that this file will be called both from server and client this.state = { error: '', username: '', password: '' }; } // I saw some people use this function, but it'll only work if // the form's method is GET static willTransitionTo(transition, params, query) { // if only we could read POST parameters here // we could do something like this transition.wait( api.post('/doLogin', postParams).then((data) => { transition.redirect(`/dashboard`); }); ); } render() { return ( <DocumentTitle title="Login"> <div className="alert alert-danger">{this.state.error}</div> <form method="post"> <input type="text" name="username" value={this.state.username} onChange={this._onFieldChange('username')} placeholder="Username" /><br /> <input type="password" name="password" value={this.state.password} onChange={this._onFieldChange('password')} placeholder="Password" /><br /> <button type="submit">Login</button> </form> </DocumentTitle> ); } _onFieldChange(name) { var self = this; return (e) => { e.preventDefault(); var nextState = {}; nextState[name] = e.target.value; self.setState(nextState); } } } 的第一个参数。

我遇到了同样的问题setting up a gulp build,我需要在脚本的目录中包含一个文件。使用path.join似乎可以优化某些路径段,因此path.join会被丢弃,即使它是路径的第一部分(并且会很重要)。

为避免这种情况,可以传递任何提供绝对路径(cwd或dirname)的内容,强制构建完整路径。