我刚刚开始进行环回开发,我只想知道如何使用环回在浏览器中设置 cookie。在nodejs的express框架中,可以通过在headers中设置cookie来发送cookie,然后再访问它。
response.writeHead(200, {
'Set-Cookie': 'mycookie=test',
'Content-Type': 'text/plain'
});
Run Code Online (Sandbox Code Playgroud)
那么如何通过环回响应来做到这一点。
谢谢。
我在/src名为 的子文件夹中创建了一个子文件夹/libs。当我运行时npm run build,子文件夹不包含在/dist.
我假设我必须修改构建脚本?如果是这样,那会是哪个文件?
编辑#1
这一切都源于试图需要位于src/libs我的控制器中的自定义模块。我尝试过各种模式:../libs/module_name, ./libs/module_name. 让它工作的唯一方法是从根(即/home/me/app/src/libs/module_name)对路径进行硬编码。
如果我这样做:console.log(__dirname)在尝试从 请求模块的控制器中/lib,我会看到对 的引用/dist。我进去一看/dist,却/libs不见踪影。
我在这里按照这个示例进行简单的日志记录中间件设置。但我似乎无法让它记录我的请求信息。当我点击 时,我得到了标准响应/ping,但没有任何内容被注销到控制台。
我尝试在两个不同的位置注册中间件。index.ts和application.ts。
// src/middleware/log.middleware.ts
import { Next } from '@loopback/core'
import { Middleware, MiddlewareContext } from '@loopback/rest'
export const testLoggingMiddleware: Middleware = async (
middlewareCtx: MiddlewareContext,
next: Next,
) => {
const { request } = middlewareCtx
console.log('Request: %s %s', request.method, request.originalUrl)
try {
// Proceed with next middleware
await next()
// Process response
console.log('Response received for %s %s', request.method, request.originalUrl)
} catch (err) {
// Catch errors from downstream middleware …Run Code Online (Sandbox Code Playgroud) 我有一个User实体和一个Hobbie实体,都在Loopback中定义了它们的模型,我在API资源管理器中看到它们.
我有一个表UserHobbie链接User和Hobbie在ManyToMany关系.我正在尝试声明一个loopback hasManyThrough关系,例如
User.hasMany(Hobbie, {through: UserHobbie});
Run Code Online (Sandbox Code Playgroud)
但我似乎无法做得好,因为它没有出现在资源管理器中.我已经/server/server.js在bootstrapping部分之后立即声明了它,并且我已经尝试过/common/User.js并且/common/Hobbie.js(但是在其中任何一个中,其他模型都不可见).
是否有正确的语法在User.json或Hobbie.json中添加它?这将是我的首选方式,因为我在json定义中的任何内容都显示在资源管理器中.
我正在编写一个远程方法,通过运行聚合管道查询可以大大增强该方法.
要做到这一点,我需要获得实际的mongodb连接并直接使用它.
我怎么能运行一些东西
module.exports = function(ZipCodes) {
ZipCodes.pipeline = function (cb) {
//Get the MongoDB Connection
var mongodbConnection = ***whatever magic***
var result = mongodbConnection.db.zipcodes.aggregate( { $group :
{ _id : "$state",
totalPop : { $sum : "$pop" } } },
{ $match : {totalPop : { $gte : 10*1000*1000 } } } );
cb(result);
};
ZipCodes.remoteMethod('pipeline', {
returns: {arg: 'zips', type: 'array', root: false},
http: {path:'/pipeline', verb: 'get'}
});
};
Run Code Online (Sandbox Code Playgroud)
我在我的datasources.json中定义了mongo
{
"db": {
"name": "db",
"connector": "memory"
}, …Run Code Online (Sandbox Code Playgroud) 我正在使用loopback开发自己的网站.但最近我遇到了hasMany remoteMethod的问题.这是问题:我有两个模型:
person.json:
{
"name": "Person",
"base": "PersistedModel",
"strict": true,
"idInjection": true,
"properties": {
/*...
....
*/
},
"validations": [],
"relations": {
"friends": {
"type": "hasMany",
"model": "Friend",
"foreignKey": "personId"
}
},
"acls": [],
"methods": []
}
Run Code Online (Sandbox Code Playgroud)
friend.json
friend.json:
{
"name": "friend",
"base": "PersistedModel",
"strict": true,
"idInjection": true,
"properties": {
/*...
....
*/
},
"validations": [],
"relations": {
},
"acls": [],
"methods": []
}
Run Code Online (Sandbox Code Playgroud)
当我调用POST/api/Persons/{id}/friends时,我想使用beforeRemote.
所以我在person.js中编码
module.exports = function(Person) {
Person.beforeRemote('__create__friends', function(ctx, instance, next) {
/*
code here
*/
}); …Run Code Online (Sandbox Code Playgroud) 我有一个有趣的问题.我正在尝试通过上传表单
<form enctype="multipart/form-data" action="/myendpoint/:id">
<input type="hidden" name="data" value="mydata" />
<input type="file" name="formname" />
</form>
Run Code Online (Sandbox Code Playgroud)
...和我的远程方法调用:
Patient.uploadVideo = function(id, mydata, cb) {
console.log(mydata);
return cb(null, { id: 123 });
};
MyModel.remoteMethod(
'uploadVideo',
{
http: {path: '/:id/recording/:recordingid/videos', verb: 'post'},
accepts: [
{arg: 'id', type: 'string', required: true},
{arg: 'mydata', type: 'object', 'http': {source: 'body'}},
]
}
);
Run Code Online (Sandbox Code Playgroud)
不幸的是身体是空白的
如何获取表单数据?我修改了server/datasources.json来包含
"storage": {
"name": "storage",
"connector": "loopback-component-storage",
"provider": "filesystem",
"root": "./server/storage"
}
Run Code Online (Sandbox Code Playgroud)
依然没有.
谢谢
我正在使用环回组件存储 将图像上传到服务器.
我想将通过服务器上传的每个图像转换为缩略图视图并将其保存到容器中.最初我使用本地文件存储来存储文件,一切正常.
在文件存储中,我使用"quickthumb"将图像转换为缩略图,然后将原始图像和缩略图大小图像保存到容器中.
但现在我想使用带有环回组件存储的amazon S3存储我的图像.通过遵循文档,我可以轻松地将图像上传到亚马逊S3桶.但我无法弄清楚如何将图像大小调整为缩略图视图,并在Amazon S3服务器上存储不同版本的图像以及原始图像.
这是我在使用文件存储实现它时所做的事情.
现在将其图像转换为缩略图大小
这是我如何使用它与loopback.
通用/模型/ container.js
module.exports = function(Container) {
var qt = require('quickthumb');
Container.afterRemote('upload', function(ctx, res, next) {
var file = res.result.files.file[0];
var file_path = "./server/storage/" + file.container + "/" + file.name;
var file_thumb_path = "./server/storage/" + file.container + "/thumb/" + file.name;
qt.convert({
src: file_path,
dst: file_thumb_path,
width: 100
}, function (err, path) {
});
next();
});
};
Run Code Online (Sandbox Code Playgroud)
但是现在要在将图像上传到S3服务器之前实现调整大小,我需要express类似语法,req.files.image …
'use strict';
module.exports = function (City) {
City.GetCurrentPopulation = function (req) {
var population;
City.app.models.Pupulation.find({where{id:req.id}}, //This line //gives me an error that cannot read property 'find' of undefined
function(req.res){
population=res.population;
});
response='Population for ' +req.cname ' is' +population;
req(null, response);
};
City.remoteMethod(
'GetCurrentPopulation', {
http: {
path: '/GetCurrentPopulation',
verb: 'GetCurrentPopulation'
},
returns: {
arg: 'startdate',
type: 'string'
}
}
);Run Code Online (Sandbox Code Playgroud)
有一个模型城市,我想访问另一个模型,如"population.find(一些过滤器)"如何做到这一点?
我有一个用城市模型编写的远程方法.我试图访问人口记录的地方
var countryp = population.find(其中{id:4});
var currentpopulation = countryp.Totalpopulation;
它给出了一个错误population.find不是一个函数.
请建议如何做到这一点.
我有一个远程方法,我将我的应用程序逻辑,如下所示:
module.exports = function(Entity) {
HcpEntity.retrieveProfile = function(body, cb) {
process.nextTick(function() {
//TODO: Application Logic
}
}
}
Run Code Online (Sandbox Code Playgroud)
相应的模型JSON片段是:
{
"name": "HcpEntity",
"base": "Model",
"properties": {},
"methods": {
"retrieveProfile": {
"isStatic" : true,
"accepts": [
{
"arg": "Request",
"type": "object",
"required": true,
"http": {
"source": "body"
}
}
],
"returns": {
"arg": "Response",
"type": "object"
},
"http": {
"verb": "post"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我需要能够访问标记为的区域中的传入HTTP标头 //TODO: Application Logic以验证它们.请有人帮忙.
loopbackjs ×10
node.js ×6
strongloop ×3
amazon-s3 ×1
cookies ×1
express ×1
javascript ×1
loopback ×1
loopback4 ×1
mongodb ×1
typescript ×1