我有一个AngularJS应用程序,其中我的代码看起来像这样:
myApp = angular.module('myApp',[ 'ui.router','ngMaterial','ngMessages' ] ); myApp.constant('CONSTANTS',(function() { // Define your variable return { backend: { baseURL: 'http://mybackend.com:3026' } }; })());
我运行这个应用程序使用http-server端口号8000这样:
% http-server -p 8000
我想传递backend.baseURL的命令行参数,以便覆盖代码中指定的值.我该怎么做??
至少需要支持动态内容的http-server.而您的http服务器仅支持静态内容.
原文链接:https://www.f2er.com/angularjs/143234.html在评论中,你问哪个服务器应该使用.有成千上万的Web服务器支持动态内容.但是你正在使用http-server的sinc我假定你只想要一个小的服务器为本地的dev.
不幸的是,我没有找到任何支持你需要的服务器,而不修改他们的代码.所以我建议你在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"
命令接受参数: