访问在loopback.js正确的请求的URL
我在我的config.json
以下配置
{
"restApiRoot": "/eth/v1",
"host": "0.0.0.0",
"port": 3001,
"remoting": {
"context": false,
"rest": {
"handleErrors": false,
"normalizeHttpPath": false,
"xml": false
},
"json": {
"strict": false,
"limit": "100kb"
},
"urlencoded": {
"extended": true,
"limit": "100kb"
},
"cors": false
}
}
而我server.js
包含以下内容:
'use strict';
var loopback = require('loopback');
var boot = require('loopback-boot');
var app = module.exports = loopback();
app.start = function() {
return app.listen(function() {
app.emit('started');
var baseUrl = app.get('url').replace(/\/$/, '');
console.log('Web server listening at: %s', baseUrl);
if (app.get('loopback-component-explorer')) {
var explorerPath = app.get('loopback-component-explorer').mountPath;
console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
}
});
};
// Bootstrap the application, configure models, datasources and middleware.
// Sub-apps like REST API are mounted via boot scripts.
boot(app, __dirname, function(err) {
if (err) throw err;
if (require.main === module) app.start();
});
以上是相当标准。我的代码如下:
var server = require('../server');
console.log(server.get('url'));
当我做curl localhost:3001
,它打印出http://localhost:3001
这是符合市场预期。
但是,如果我加入我的/etc/hosts
文件以下
127.0.0.1 localhost locahost-testnet
并尝试访问这样curl localhost-testnet:3001
,它仍然打印http://localhost:3001
这不是我所期望的。
我应该怎么做才能访问的网址是什么? (http://localhost-testnet:3001
)
您可以通过定义为remoteHook模型类似下面得到它:
yourModel.beforeRemote('**', function(ctx, user, next) {
console.log(ctx.req.headers.host);
// this will print "localhost-testnet:3001" in your case
next();
});
使用通配符远程钩子运行被称为任何远程方法之前。 Doc