我有一个AngularJS应用程序,我的代码看起来像这样:
myApp = angular.module('myApp',
[
'ui.router',
'ngMaterial',
'ngMessages'
]
);
myApp.constant('CONSTANTS', (function() {
// Define your variable
return {
backend: {
baseURL: 'http://mybackend.com:3026'
}
};
})());
Run Code Online (Sandbox Code Playgroud)
我在端口号8000上使用http-server运行此应用程序,如下所示:
% http-server -p 8000
Run Code Online (Sandbox Code Playgroud)
我想传递一个命令行参数,backend.baseURL以便它超越代码中指定的值.我该怎么做??
您至少需要支持动态内容的http服务器。而您的 http 服务器仅支持静态内容。
在评论中您询问应该使用哪个服务器。有数以千计的网络服务器支持动态内容。但由于您当前正在使用,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);
Run Code Online (Sandbox Code Playgroud)
然后你可以通过以下方式运行你的服务器
nodejs myserver.js /mydocument/myproject/ 8000 config.js "http://mybackend.com:3026" "http://mydevserver.com:80"
Run Code Online (Sandbox Code Playgroud)
该命令接受参数:
该服务器仅支持一个动态文件,具有简单的查找/替换功能。从这一点来看,我想你可以修改中间件来做你想做的事情。
| 归档时间: |
|
| 查看次数: |
2765 次 |
| 最近记录: |