- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在服务器端 js 文件中,例如 send-email.js
,我正在尝试创建一个超链接来发送电子邮件
var config = require('../../server/config.json');
function sendEmail() {
var url = config.protocol + '://' + config.host + ':' + config.port + '/login';
//code to send e-mail follows...
}
我在 config.json 文件中配置了协议(protocol)、主机和端口,并使用它们我能够成功创建超链接。
我也在使用boot scripts LoopbackJS 的,其中 app
参数的传递如下代码所示:
module.exports = function (app, cb) {
var url = 'http://' + app.get('host') + ':' + app.get('port') + '/login';
}
我可以获取主机和端口,但不能获取协议(protocol)。
现在有两种方式获取主机和端口:
config.json
文件app.get
方法没有 app.get('protocol')
来获取协议(protocol),因此我在 config.json
文件中配置了它,但我想自动获取协议(protocol)以防止在部署到服务器时更改文件。
是否有类似根据此 Nodejs 应用程序的托管位置自动获取协议(protocol)(或主机和地址)之类的内容,而不是对其进行配置。
我愿意接受来自 NodeJS、ExpressJS 或 LoopbackJS 角度的答案。
最佳答案
如果您的主要目标是避免在部署时更改配置文件,那么还有另一种选择。
不要在代码中获取协议(protocol),而是在将运行应用程序的每个主机上设置一个环境变量。然后在 config.local.js 中覆盖 config.json 中的“协议(protocol)”设置。
配置.json:
{
"restApiRoot": "/api",
"host": "0.0.0.0",
"port": 3000,
"protocol": "http"
...
}
config.local.js:
module.exports = {
"host": process.env.APPNAME_API_HOST,
"port": process.env.APPNAME_API_PORT,
"protocol": process.env.APPNAME_API_PROTOCOL
...
}
运行应用程序的用户的服务器引导 config.sh 或 .bashrc(如果是 bash):
export APPNAME_API_HOST=0.0.0.0
export APPNAME_API_PORT=3333
export APPNAME_API_PROTOCOL=https
(如果您使用 Strong-pm 运行,则可以使用 slc ctl servicename env-set ENV=value
命令设置这些值。)
这允许您在设置变量的任何环境中使用相同的 config.local.js 文件,并且环境配置是特定于主机的。无需更改文件,服务器环境配置也可以自动化并 checkin 。
关于node.js - 在 LoopbackJS : How to get server protocol (http or https) in a boot script or server side code? 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33951770/
我在工作中使用 strongloop 的 loopbackjs 来实现 API。 对于模型 Cat我已经定义了一个远程方法,我们称之为 meow . 所以我可以这样做: GET /cats/{:id}
我在这样的环回中继承了一个模型- { "name": "MyModel", "base": "AnotherModel", "idInjection": false,
是否可以使用回送电子邮件数据源发送附件? 我在源文档中只能看到这些字段: @property {String} 到电子邮件收件人。必需的。 @property {String} 来自电子邮件发件人地址
我有两个通过 hasMany 关系关联的模型。 Customer hasMany CustomerPhones 创建新的 Customer 时,我想将相关的 CustomerPhones 作为单个请求
如何将用户、角色、用户角色映射等内置模型移动到我们创建的数据库而不是默认数据源:db?内置模型未在 Arc 中列出。 我尝试创建一个继承这些基本模型的新模型。但是,数据不会保存到新模型中。 请建议..
环回使用序列号作为型号ID。我可以在服务器端使用自己的ID生成器吗?我该怎么做呢? 最佳答案 可以在model definition file中将回送生成器(guid,uuid等)指定为id属性的默认
好吧,互联网公民,我需要一些帮助......我的 super 简单远程方法没有触发回调。相反,我收到此错误消息: /maestro/common/models/datalog.js:11
我正在使用环回进行 API 设计和数据建模。我使用 MySQL 作为我的数据库。尽管我的 API rest URL 成功返回了结果,例如/states/{id}/cities .我有以下模型,但似乎没
回送有两个区域,这些区域为静态文件设置了路径: server.js var path = require('path'); app.use(loopback.static(path.res
对于模型定义 product.js,我的 api 端点如下所示 api/products/9720?id_shop=1&id_lang=1 我需要访问 product.js 中的 id_shop 以在
我希望能够根据相关模型的属性进行过滤。例如,我有一个带有 Building 对象的 Class 对象。我想获得在某个建筑物中进行的类(class)列表。 这个过滤器 { "include":"
是否有规定的方法在环回中创建自定义验证器?例如,假设我想创建类似的东西: Validatable.validatesRange('aProperty', {min: 0, max: 1000}) 请注
我正在尝试向模型添加非静态远程方法。只需关注 code这里。不幸的是,我收到了一些错误消息。 以下是我的代码 User.prototype.lastOrder = function(callback)
我想做的事:有一个 html 表单,里面有一个文件输入。选择文件后,文件输入应上传文件,并获取文件 ID,因此提交表单时,文件 ID 会随表单一起发布并写入数据库。 简短版本:我想将元数据(例如 ID
是否有关于环回中错误处理的完整、一致且有据可查的信息来源? 诸如错误代码及其含义、与 http 状态的关系之类的东西。我已经阅读了他们的文档,但没有发现类似的内容。 我想翻译所有消息,以便为我的应用程
我有一个类似于下面的博客文章列表 [ { title: "Post #1", tags: ["foo", "bar"] }, {
'use strict'; module.exports = function (City) { City.GetCurrentPopulation = function (req) { var po
如何保证环回模型中多个字段组合的唯一性。下面是模型组织,我有两个字段名称和联系人,我希望这两个字段的组合在数据库中是唯一的。 例如:- 在创建组织时,两条记录可以在 中具有相同的值。 '姓名'字段而是
对于 LoopBack3,有一个帮助主题描述了如何将数据库更改实时流式传输到客户端:https://loopback.io/doc/en/lb3/Realtime-server-sent-events
我们的 MySQL 数据库设置了写集群和读集群,有没有办法设置 Strongloop Loopback 模型(例如用户)来写入 MySQL 主机 A 并从 MySQL 主机 B 读取? 最佳答案 如果
我是一名优秀的程序员,十分优秀!