小编Yil*_*maz的帖子

为什么反应堆设计模式名为reactor?

我们可以查看描述反应堆设计模式的原始论文.

反应器 - siemens.pdf

Reactor设计模式处理由一个或多个客户端同时传递给应用程序的服务请求.应用程序中的每个服务可能包含多个方法,并由一个单独的事件处理程序表示,该处理程序负责调度特定于服务的请求.事件处理程序的调度由启动调度程序执行,该调度程序管理已注册的事件处理程序.服务请求的解复用由同步事件解复用器执行.

但我仍然无法理解为什么命名反应堆?反应堆是什么意思?

design-patterns

3
推荐指数
1
解决办法
290
查看次数

在没有 AuthGuard 的路由上获取 nestjs 中的当前用户

我使用带有 jwt 策略的带护照的 Nestjs。我想根据我的一些请求获得当前用户。目前,我有一个看起来像这样的装饰器:

import { createParamDecorator, ExecutionContext } from '@nestjs/common';

export const CurrentUser = createParamDecorator(
  (data: string, ctx: ExecutionContext) => {
    const user = ctx.switchToHttp().getRequest().user;

    if (!user) {
      return null;
    }

    return data ? user[data] : user; // extract a specific property only if specified or get a user object
  },
);
Run Code Online (Sandbox Code Playgroud)

当我在带有 AuthGuard 的路线上使用它时,它按预期工作:

@Get('test')
  @UseGuards(AuthGuard())
  testRoute(@CurrentUser() user: User) {
    console.log('Current User: ', user);
    return { user };
  }
Run Code Online (Sandbox Code Playgroud)

但是我如何让它在非保护路线上工作(获取当前用户)?我需要用户无论是否获得授权都能够发表他们的评论,但是,当他们登录时,我需要得到他们的名字。

基本上,我需要一种方法来在每个(或至少在一些不是 AuthGuard 的请求上)传播 req.user,通过应用护照中间件在 express 中做真的很简单,但我不知道该怎么做它与@nestjs/passport。

[编辑] 感谢 …

node.js typescript passport.js nestjs nestjs-passport

3
推荐指数
1
解决办法
3406
查看次数

getStaticProps + Fallback:true 和 getServerSideProps 之间到底有什么不同?

我多次阅读Next.js 文档,但我仍然不知道getStaticProps使用fallback:true 和getServerSideProps.

据我所理解 :

获取静态属性

getStaticProps在构建时呈现,并将任何请求作为静态 HTML 文件提供。它与不经常更新的页面一起使用,例如“关于我们”页面。

export async function getStaticPaths() {

return {
    paths: [{ params: { id: '1' } }, { params: { id: '2' } }]
  }
}
Run Code Online (Sandbox Code Playgroud)

但是如果我们放在fallback:true函数的返回处,并且有一个对构建时未生成的页面的请求,Next.js 会将该页面生成为静态页面,然后该页面上的其他请求将作为静态页面静止的。

export async function getStaticPaths() {

return {
    paths: [{ params: { id: '1' } }, { params: { id: '2' } }],
    fallback: true,
  }
}
Run Code Online (Sandbox Code Playgroud)

所以,getStaticProps's这个概念对我来说非常有效。我认为它可以适用于大多数场景。但如果getStaticProps效果很好,那我们为什么需要呢getServerSideProps

据我所知,如果我们使用 …

javascript reactjs server-side-rendering next.js

3
推荐指数
1
解决办法
2240
查看次数

在auth0中获取accessToken

我正在使用auth0nextJS

我下一步想做:当用户添加他的凭据并登录时,他将被重定向到callbackAPI。

和这里

    import auth0 from '../../utils/auth0';

    export default async function callback(req, res) {
      try {
        await auth0.handleCallback(req, res, {
          redirectTo: '/'
        });
      } catch (error) {
        console.error(error);
        res.status(error.status || 400).end(error.message);
      }
    }
Run Code Online (Sandbox Code Playgroud)

我想根据令牌重定向用户。
如果应用程序是简单的用户或管理员,则解码令牌我将获取数据。

如果他是管理员,即使不是用户页面,他也应该被重定向到管理页面。

所以我做了这样的事情:

    import auth0 from '../../utils/auth0';

    export default async function callback(req, res) {
       const tokenCache = auth0.tokenCache(req, res);
       const { accessToken } = await tokenCache.getAccessToken();
       console.log(accessToken) 
      try {
        await auth0.handleCallback(req, res, { redirectTo: '/' });
      } catch (error) { …
Run Code Online (Sandbox Code Playgroud)

javascript authentication reactjs auth0 next.js

3
推荐指数
1
解决办法
6635
查看次数

当我在“sanity init”成功后尝试执行“sanity start”时出错

我正在尝试安装 Sanity studio,我按照以下步骤操作:

npm install --global @sanity/cli

sanity install

sanity init

在初始化步骤之后,我尝试运行下一步:

sanity start

但它说:

Run the command again within a Sanity project directory, where "@sanity/core"
is installed as a dependency.
    at D.runCommand (F:/All Node and related projects/node_modules/@sanity/cli/b
in/sanity-cli.js:3254:1345)
    at t.exports (F:/All Node and related projects/node_modules/@sanity/cli/bin/sanity-cli.js:1794:2419)
Run Code Online (Sandbox Code Playgroud)

但是当我尝试 cd 进入我的 sanity 项目目录时,它甚至无法识别 sanity,它说:

'sanity' is not recognized as an internal or external command,
operable program or batch file.
Run Code Online (Sandbox Code Playgroud)

我的依赖目录结构不正确吗?需要满足什么条件才可以打电话 sanity start

谢谢!

sanity

3
推荐指数
1
解决办法
1万
查看次数

如何在自定义 css 类中使用 tailwindcss 颜色?

我可以使用预定义的tailwind类在 HTML 中设置颜色,例如:

<div class="border border-purple-500"></div>
Run Code Online (Sandbox Code Playgroud)

但我也想在我的自定义 CSS 中使用相同的颜色,例如:

.my-class {
    border: 1px solid $purple-500;
}
Run Code Online (Sandbox Code Playgroud)

是否可以在CSS中获取顺风颜色值?

css tailwind-css

3
推荐指数
1
解决办法
846
查看次数

Solidity:错误:请将数字作为字符串或 BN 对象传递,以避免精度错误

有一个简单的 Solidity 合约:

contract SellStuff{

    address seller;
    string name;
    string description;
    uint256 price;

    function sellStuff(string memory _name, string memory _description, uint256 _price) public{
        seller = msg.sender;
        name = _name;
        description = _description;
        price = _price;
    }
    function getStuff() public view returns (
        address _seller, 
        string memory _name, 
        string memory _description, 
        uint256 _price){
            return(seller, name, description, price);
    }
}
Run Code Online (Sandbox Code Playgroud)

并运行 javascript 测试,如下所示:

var SellStuff= artifacts.require("./SellStuff.sol");

// Testing
contract('SellStuff', function(accounts){

    var sellStuffInstance;
    var seller = accounts[1];
    var stuffName = "stuff 1";
    var …
Run Code Online (Sandbox Code Playgroud)

javascript blockchain ethereum solidity smartcontracts

3
推荐指数
2
解决办法
1万
查看次数

django-cors-headers 不允许来自允许来源的请求

我面临的问题是我无法从 NextJS 前端获取现有用户。我使用的后端框架是 Django(以及 django-cors-headers 包)。django-cors-headers 不允许某个 HTTP 请求,但它应该允许。

\n

我的 next.config.js 包含重写,以便我可以访问我的后端。

\n
async redirects() {\n    return [\n      {\n        source: \'/api/:path*\',\n        destination: \'http://localhost:8000/:path*/\',\n        permanent: true,\n      },\n    ]\n  },\n
Run Code Online (Sandbox Code Playgroud)\n

我的 django-cors-headers 设置如下所示:

\n
# CORS\n\nCSRF_TRUSTED_ORIGINS = [\n    \'http://localhost:3000\',\n]\n\nCORS_ALLOWED_ORIGINS = [\n    \'http://localhost:3000\',\n    \'http://localhost:8000\',\n    \'http://127.0.0.1:3000\',\n    \'http://127.0.0.1:8000\',\n]\n\nCORS_ALLOW_ALL_ORIGINS = True\n
Run Code Online (Sandbox Code Playgroud)\n

失败的请求尝试获取 ID 为 1 的用户。该用户存在,因此该请求应该成功。

\n
fetch(`/api/users/${String(userId)}/`, {\n    mode: \'cors\',\n    credentials: \'include\',\n    headers: {\n      \'Content-Type\': \'application/json\',\n    },\n  })\n
Run Code Online (Sandbox Code Playgroud)\n

但是,我从请求中得到的唯一结果是有关 CORS 的错误消息。

\n
Cross-Origin Request Blocked: The Same Origin Policy disallows reading …
Run Code Online (Sandbox Code Playgroud)

python django django-cors-headers fetch-api next.js

3
推荐指数
1
解决办法
1万
查看次数

Solidity:为什么使用初始化函数而不是构造函数?

我正在做审计智能合约,有人更喜欢使用这样的初始化函数:

 bool private isInit=false;
 string private hello;
 
 function init(string _hello) public onlyOwner {
   hello = _hello;
   isInit = true;
 } 

 function doSomething() public {
   require(isInit, "Wait for initialize");
   ...doSomething
 }
Run Code Online (Sandbox Code Playgroud)

你能解释一下为什么没有使用构造函数吗?

architecture state ethereum solidity smartcontracts

3
推荐指数
1
解决办法
4388
查看次数

LRU 缓存为何比 hashmap 更快?

除了 LRU 缓存的结构之外,我还没有读过太多关于它的内容,但我仍然对它比常规哈希图快得多感到惊讶。

我做了一个测试,一个递归组合问题,使用常规哈希图来保存递归期间的结果结果(动态编程),并做了相同的操作,唯一的区别是使用了 LRU 缓存实现(大小 1024)。

性能从1秒下降到0.006秒!

现在,这非常令人惊讶,我不知道为什么会这样。对于大多数操作来说,哈希图的时间复杂度为 O(1),并且 LRU 缓存需要哈希双向链表。

语境:

  • 我在这个项目中使用 C++。所讨论的 hashmap 是一个 unordered_map,以字符串作为键,以整数作为值。我听说过 unordered_map 最坏情况的复杂度为NN 2,但据我所知,它通常在O(1)中执行所有操作。

  • LRU 缓存实现是从堆栈溢出复制粘贴的:D

代码

使用 LRU 缓存

#include <bits/stdc++.h>

using namespace std;
using namespace std::chrono;

template <typename T,typename U>                                                   
std::pair<T,U> operator+(const std::pair<T,U> & l,const std::pair<T,U> & r) {   
    return {l.first+r.first,l.second+r.second};                                    
}
#pragma GCC optimize ("Ofast")
#pragma GCC target ("avx2")


// LRU Cache implementation
template <class KEY_T, class VAL_T> class LRUCache{ …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm computer-science dynamic-programming data-structures

3
推荐指数
1
解决办法
1383
查看次数