在某些情况下使用 async/await 比使用 Promise 慢吗?
考虑使用 promise 的此类代码
function foo() {
const p1 = new Promise(res => setTimeout(() => res('first'), 2000));
const p2 = new Promise(res => setTimeout(() => res('second'), 2000));
p1.then(console.log);
p2.then(console.log);
}
foo()
Run Code Online (Sandbox Code Playgroud)
2000 毫秒后'first',然后'second'打印到控制台。
使用 async/await 的相同代码
async function foo() {
const p1 = await new Promise(res => setTimeout(() => res('first'), 2000));
const p2 = await new Promise(res => setTimeout(() => res('second'), 2000));
console.log(p1);
console.log(p2);
}
foo();
Run Code Online (Sandbox Code Playgroud)
使用 async/await 打印'first'和等待需要 4000 …
我有一个带有两个矩形的简单 SVG。我希望“内部”矩形正好位于 SVG 的中间。通过设置x和y属性以50%左上角为中心。相反,我想将矩形的中间居中。我试过设置transform-origin为,center但它不起作用。
<svg width="100" height="100">
<rect width="100%" height="100%" fill="gold" />
<rect width="30" height="30" x="50%" y="50%" fill="green" />
</svg> Run Code Online (Sandbox Code Playgroud)
如何在不手动指定 x 和 y 属性的情况下实现此类功能?
当我运行时,docker-compose build我看到 package.json 的依赖项已安装在容器上,但没有安装在我的计算机上。因此,当我运行之后,docker-compose up我收到错误,提示找不到依赖项,即:Error: Cannot find module 'express'
我的应用程序结构非常简单,如下所示:
|____frontend
| |____index.html
| |____index.js
| |____webpack.config.js
| |____yarn.lock
| |____package.json
| |____dev.Dockerfile
|____backend
| |____server.js
| |____yarn.lock
| |____package.json
| |____dev.Dockerfile
|____docker-compose.yml
Run Code Online (Sandbox Code Playgroud)
我的 docker-compose.yml:
version: "3.3"
services:
frontend:
build:
context: .
dockerfile: frontend/dev.Dockerfile
volumes:
- ./frontend:/frontend:cached
command: yarn start
backend:
build:
context: .
dockerfile: backend/dev.Dockerfile
volumes:
- ./backend:/backend:cached
command: yarn start
Run Code Online (Sandbox Code Playgroud)
这是前端的 Dockerfile(后端看起来相同):
FROM node:latest
WORKDIR "/frontend/"
COPY frontend/package.json frontend/package.json
COPY frontend/yarn.lock frontend/yarn.lock
RUN …Run Code Online (Sandbox Code Playgroud) 我有一个带有常规浏览器路由器(没有哈希)的单页应用程序。每当有人浏览页面并点击刷新按钮时,nginx 都会尝试在此路径上查找文件。所以如果有人在mypage.com/aboutnginx 上寻找about文件并返回 404 Not Found 响应。如何解决这个问题?
我正在考虑使用通配符指定位置 -mypage.com/*不过/api,因为此应用程序中的每个后端端点都以api. 如何匹配除一个之外的所有路径?这就是我的配置的样子:
upstream frontend {
server frontend:3000;
}
upstream backend {
server backend:8000;
}
server {
listen 80;
location /api {
proxy_pass http://backend;
proxy_set_header Host \$http_host;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
}
location / {
proxy_pass http://frontend;
proxy_redirect default;
}
}
Run Code Online (Sandbox Code Playgroud)