2016-12-28 81 views
15

我有一個AngularJS應用在我的代碼看起來是這樣的:如何使角應用程序從命令行獲取參數?

myApp = angular.module('myApp', 
    [ 
    'ui.router', 
    'ngMaterial', 
    'ngMessages' 
    ] 
); 

myApp.constant('CONSTANTS', (function() { 
    // Define your variable 
    return { 
    backend: { 
     baseURL: 'http://mybackend.com:3026' 
    } 
    }; 
})()); 

我運行的端口號8000使用http-server這樣該應用:

% http-server -p 8000 

我想在一個命令傳遞在線參數爲backend.baseURL,使其覆蓋代碼中指定的值。我該怎麼做??

+1

您需要的是至少需要支持動態內容的http-server。而你的'http-server'只支持靜態內容。 – Hereblur

+0

Hereblur,你能告訴我如何?我應該使用哪個服務器? –

+0

我認爲這個配置應該是你的構建設置的一部分,而不是服務器設置。 – gvmani

回答

3

您需要的是至少需要支持動態內容的http-server。而你的http服務器僅支持靜態內容。

並在評論中詢問你應該使用哪個服務器。有數千個支持動態內容的網絡服務器。但你正在使用sinc http-server我認爲你只是想爲本地開發小型服務器。

不幸的是,我找不到任何支持您的需求的服務器,而無需修改它們的代碼。所以我建議你在npm上的庫上創建你自己的服務器庫。

這是和示例服務器使用live-server

var liveServer = require("live-server"); 
var fs = require("fs") 

var root = process.argv[2] || "." 
var port = process.argv[3] || 8000 

var replaceTextMiddleWare = function(req, res, next){ 

    var file = process.argv[4] 
    var find = process.argv[5] 
    var replace = process.argv[6] 

    if(file && find){ 
     if(req.url === file) { 
       fs.readFile(root + file, "utf-8", function(e, content){ 
        res.end(content.replace(find, replace)) 
       }) 

       return; 
     } 
    } 


    next(); 
} 

var params = { 
    port: port, // Set the server port. Defaults to 8080. 
    host: "0.0.0.0", // Set the address to bind to. Defaults to 0.0.0.0 or process.env.IP. 
    root: root, // Set root directory that's being server. Defaults to cwd. 
    open: false, // When false, it won't load your browser by default. 
    ignore: 'scss,my/templates', // comma-separated string for paths to ignore 
    file: "index.html", // When set, serve this file for every 404 (useful for single-page applications) 
    wait: 1000, // Waits for all changes, before reloading. Defaults to 0 sec. 
    mount: [['/components', './node_modules']], // Mount a directory to a route. 
    logLevel: 2, // 0 = errors only, 1 = some, 2 = lots 
    middleware: [ replaceTextMiddleWare ] // Takes an array of Connect-compatible middleware that are injected into the server middleware stack 
}; 


liveServer.start(params); 

然後你就可以通過

nodejs myserver.js /mydocument/myproject/ 8000 config.js "http://mybackend.com:3026" "http://mydevserver.com:80" 

運行服務器的命令接受參數:

  • 路徑提供內容
  • 端口
  • 文件名
  • 文本找到
  • 要替換的文本

此服務器僅支持一個帶簡單查找/替換的動態文件。 從這一點來說,我猜你可以修改中間件來做你想做的任何事情。

1

當伊夫這樣做在生產我用這個生成過程中,在這種情況下使用一飲而盡,

var knownOptions = { 
    string: 'env', 
    default: { env: process.env.NODE_ENV || 'default' } 
    }; 

    var options = minimist(process.argv.slice(2), knownOptions); 

    console.log("using config : " + chalk.blue(options.env)); 

我們得到了一個環境變量默認使用minimist拖欠我們可以通過-env「串」

然後進一步在代碼推動態文件到app.js

//now we use options.env 
    appJS.push("env/"+options.env+".js"); 

ENV/[options.env]的.js這裏是出口環境specifi的角模塊c常量

1

看起來你不是使用gulp,而是使用package.json中的節點腳本。如果你使用gulp,那麼這應該不是你通過gulp使用http-server的問題。 你現在可以做的一件事是作爲你的運行命令的一部分,設置process.env。的baseUrl =「動態」,然後粗略地講,在你的代碼中使用此類似這樣的

return { 
backend: { 
    baseURL:process.env.baseUrl || 'http://fallbackurl' 
} 
1

基本上我的理解是,你要自定義HTTP-server軟件包爲您贏得了自定義處理。

這裏是小的解決方案,我發現你....

轉到節點安裝文件夾(在我的情況下,當地一個)......

所以我們要編輯文件的路徑像這樣...

../node_modules/http-server/lib/http-server.js

導入一個軟件包,爲您服務,以獲取命令行參數......你不需要安裝它,它已經在那裏。之後行號

var argv = require('optimist').boolean('cors').argv; // add this line at前7

現在

在第60行和代碼before.push(function (req, res) {添加以下代碼後..

if(req.url === '/backend-url') { 
    return res.end(JSON.stringify(argv.x)); 
} 

所以推送功能之前的樣子::

before.push(function (req, res) { 
    if(req.url === '/backend-url') { 
     return res.end(JSON.stringify(argv.x)); 
    } 
    if (options.logFn) { 
     options.logFn(req, res); 
    } 

    res.emit('next'); 
    }); 

現在我們已經配置了新的命令行參數x對於HTTP服務器,這將對於網址「/後端-URL」

現在,在前端

myApp.constant('CONSTANTS', ('$http', function($http) { 
// Define your variable 
**var backendURL = (function() { 
    return $http.get('/backend-url', function(bUrl) { 
     return bUrl; 
    }) 
})();** 
return { 
    backend: { 
     baseURL: backendURL 
    } 
}; 
})()); 

Done, now you add url like this: **http-server -p 8080 -x http://example.com** 

I choose this approch as replacing file content i dont think good in you case 
1

如果您使用的只有一個實例被返回您的應用程序,您可以使用腳本啓動它,它接受所有必要的命令行參數,在應用程序js文件中替換適當的佔位符(例如特定註釋之間的字符串),然後在必要的端口上啓動http-server。

相關問題