小编noo*_*ook的帖子

Nuxt:仅在服务器端获取数据

我使用 Github 的 API 来获取固定存储库的列表,并将调用放入 AsyncData 方法中,以便在第一次渲染时获得该列表。但我刚刚了解到 AsyncData 在服务器端调用一次,然后每次在客户端加载页面时调用。这意味着客户端不再拥有进行 API 调用的令牌,无论如何,我不会让我的 Github 令牌出现在客户端中。

当我切换页面(从另一个页面到带有列表的页面)时,数据不存在,我只有默认的空数组

我不知道确保我的数据始终加载在服务器端的最佳方法是什么?

export default defineComponent({
  name: 'Index',
  components: { GithubProject, Socials },
  asyncData(context: Context) {
    return context.$axios.$post<Query<UserPinnedRepositoriesQuery>>('https://api.github.com/graphql', {
      query,
    }, {
      headers: {
        // Token is defined on the server, but not on the client
        Authorization: `bearer ${process.env.GITHUB_TOKEN}`,
      },
    })
      .then((data) => ({ projects: data.data.user.pinnedItems.nodes }))
      .catch(() => {});
  },
  setup() {
    const projects = ref<Repository[]>([]);

    return {
      projects,
    };
  },
});
Run Code Online (Sandbox Code Playgroud)

vue.js server-side-rendering nuxt.js

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

带有嵌套属性的打字稿判别联合,具有一长串可能性

我在一些地方读到,来自嵌套属性的判别式联合目前不可行,或者至少在没有保护函数的情况下不可行,我想知道是否有一种方法根本不使用保护函数来确定类型。

目标是尝试使用 Enum 来缩小范围,因为多个CustomFieldType可以具有相同类型的value,并且每次使用 switch 语句注册案例比if-elseif缩小范围更简单。

这是一个尝试不同可能性的打字稿游乐场。

enum CustomFieldType {
    INT = 'int',
    STRING = 'string',
    MULTISELECT = 'multiselect',
    SELECT = 'select'
    // Possibly 20 others
}

interface CustomFieldValueBase<T extends CustomFieldType> {
    customField: {
        type: T;
    }
    value: any;
}

interface CustomFieldValueInt extends CustomFieldValueBase<CustomFieldType.INT> {
    value: number;
}
interface CustomFieldValueString extends CustomFieldValueBase<CustomFieldType.STRING> {
    value: string;
}
interface CustomFieldValueMultiSelect extends CustomFieldValueBase<CustomFieldType.MULTISELECT> {
    value: number[];
}
interface CustomFieldValueSelect extends CustomFieldValueBase<CustomFieldType.SELECT> {
    value: number[];
}

type …
Run Code Online (Sandbox Code Playgroud)

discriminated-union typescript

5
推荐指数
0
解决办法
339
查看次数

在不同端口上使用 VS Code 调试 2 个 nodemon 实例

我目前正在完全使用 Node.js 开发一个应用程序,因此有 2 个 nodemon 实例同时运行client,分别是server

.
|-- README.md
|-- client
|   |-- index.js
|   |-- node_modules
|   |-- package-lock.json
|   `-- package.json
`-- server
    |-- index.js
    |-- node_modules
    |-- package-lock.json
    `-- package.json

4 directories, 7 files
Run Code Online (Sandbox Code Playgroud)

所以这是在 VS Code 中打开的目录。在scripts两者的部分中,package.json我有以下内容: "dev": "nodemon --inspect ./index.js"

我对如何调试 nodemon 实例进行了一些研究,我在 VS Code 的存储库中找到了这个配置:

"configurations": [
    {
        "type": "node",
        "request": "attach",
        "name": "Node: Nodemon",
        "processId": "${command:PickProcess}",
        "restart": true,
        "protocol": "inspector",
    }
]
Run Code Online (Sandbox Code Playgroud)

但现在的问题是,当我同时运行这两个脚本时,我的终端出现以下错误: Starting …

nodemon visual-studio-code vscode-debugger

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

在中间件中获取响应状态代码

我正在构建一个应用程序,其中我试图为每个请求构建自己的日志记录系统。

对于每个请求,我想记录时间戳,使用的方法,路由,最后是已发送到客户端的响应代码。

我现在有以下代码:

// index.js
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');

const app = express();
app.use(bodyParser.json());
app.use(cors());

app.use(require('./lib/logging'));

app.get('/', (req, res, next) => {
  res.send('hello world !');
});

app.listen(3001);

// ./lib/logging.js
const moment = require('moment');
const chalk = require('chalk');
const log = console.log;

module.exports = (req, res, next) => {
  let now = `[${chalk.green(moment().format('HH:mm:ss'))}]`;
  let method = chalk.magenta(req.method);
  let route = chalk.blue(req.url);
  let code = chalk.yellow(res.statusCode); // Always 200
  log(`${now} ${method} request received on …
Run Code Online (Sandbox Code Playgroud)

javascript middleware http-status-codes node.js express

2
推荐指数
2
解决办法
1225
查看次数

Javascript减少分配和返回隐式返回

我想从我的对象中删除值等于 的键null

我利用了与减速器结合使用的filter功能Object.entries

我设法创建了这个片段,但它有一个漏洞:如果任何键包含一个 falsey 值,reducer 将返回错误的值:

const obj = {
  boolKey: true,
  intKey: 1,
  nullKey: null,
  falseyKey: 0,
  stringKey: 'string',
};

const result = Object.entries(obj)
  .filter(([, value]) => value !== null)
  .reduce((accumulator, [key, value]) => (accumulator[key] = value) && accumulator, {});

console.log(result); // 0
Run Code Online (Sandbox Code Playgroud)

但是有了这个对象,结果正如预期的那样:

const obj = {
  boolKey: true,
  intKey: 1,
  nullKey: null,
  stringKey: 'string',
};

const result = Object.entries(obj)
  .filter(([, value]) => value !== null)
  .reduce((accumulator, [key, value]) => (accumulator[key] …
Run Code Online (Sandbox Code Playgroud)

javascript ecmascript-6

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