- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Axios NuxtJS config 之后,我在我的 nuxt.config.js
上创建了这样的代理配置:
proxy: {
'/api/': {
target: 'https://myapidomain.com/',
pathRewrite: { '^/api/': '' },
changeOrigin: true
}
}
此配置在 dev
环境中完美运行,包括服务器端渲染和客户端渲染。这是我们用来创建 api 包装器 api.js
的代码:
export default (context, inject) => {
inject('api', {
getPageForSlug: (slugRoute) => {
return context.$axios.$get(`/api/pageForSlug?routeName=${slugRoute}`)
},
})
}
然后从任何 vue 类:
const response = await app.$api.getPageForSlug(params.slug_route)
唯一的问题发生在从 zeit now
部署调用此代码时。客户端工作正常,但服务器端返回此错误:
START RequestId: dd92dbad-135f-414b-bc1d-df9faffaa681 Version: $LATEST
2019-08-30T17:46:03.098Z dd92dbad-135f-414b-bc1d-df9faffaa681 INFO λ Cold start took: 5265.617811ms
END RequestId: dd92dbad-135f-414b-bc1d-df9faffaa681
REPORT RequestId: dd92dbad-135f-414b-bc1d-df9faffaa681 Duration: 438.53 ms Billed Duration: 500 ms Memory Size: 3008 MB Max Memory Used: 92 MB
N',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
validateStatus: [Function: validateStatus],
data: undefined },
request:
Writable {
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: true,
autoDestroy: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
domain: null,
_events:
[Object: null prototype] { response: [Function], error: [Function] },
_eventsCount: 2,
_maxListeners: undefined,
_options:
{ protocol: 'http:',
maxRedirects: 21,
maxBodyLength: 10485760,
path: '/api/pageForSlug?routeName=lud_form',
method: 'GET',
headers: [Object],
agent: undefined,
auth: undefined,
hostname: 'localhost',
port: '3000',
nativeProtocols: [Object],
pathname: '/api/pageForSlug',
search: '?routeName=lud_form' },
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 0,
_requestBodyBuffers: [],
_onNativeResponse: [Function],
_currentRequest:
ClientRequest {
domain: null,
_events: [Object],
_eventsCount: 6,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Socket],
connection: [Socket],
_header:
'GET /api/pageForSlug?routeName=lud_form HTTP/1.1\r\nAccept: application/json, text/plain, */*\r\nx-now-deployment-url: lps-5fxmi58fz.now.sh\r\nx-now-trace: staging-gru1\r\nx-real-ip: 177.45.65.235\r\nx-zeit-co-forwarded-for: 177.45.65.235\r\nupgrade-insecure-requests: 1\r\nx-forwarded-proto: https\r\nx-now-id: 7xllz-1567187175018-d0ce35475a9e\r\naccept-encoding: gzip, deflate\r\nuser-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36\r\nx-forwarded-for: 177.45.65.235\r\nx-forwarded-host: lps-danicuki.playax.now.sh\r\ndnt: 1\r\naccept-language: en-US,en;q=0.9\r\nconnection: close\r\nHost: localhost:3000\r\n\r\n',
_onPendingData: [Function: noopPendingOutput],
agent: [Agent],
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/api/pageForSlug?routeName=lud_form',
_ended: false,
res: null,
aborted: undefined,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
_redirectable: [Circular],
[Symbol(isCorked)]: false,
[Symbol(outHeadersKey)]: [Object] },
_currentUrl: 'http://localhost:3000/api/pageForSlug?routeName=lud_form' },
response: undefined,
isAxiosError: true,
toJSON: [Function] }
END RequestId: 6e355938-32d5-459a-adf7-08fb97101e29
REPORT RequestId: 6e355938-32d5-459a-adf7-08fb97101e29 Duration: 281.53 ms Billed Duration: 300 ms Memory Size: 3008 MB Max Memory Used: 117 MB
RequestId: 6e355938-32d5-459a-adf7-08fb97101e29 Error: Runtime exited with error: exit status 1
Runtime.ExitError
如何使服务器端 API 请求适用于所有环境?
最佳答案
这可以通过在 now.json
Vercel 文件中使用 rewrite
来实现。无需破解。
{
"version": 2,
"builds": [
{
"src": "nuxt.config.js",
"use": "@nuxtjs/vercel-builder",
"config": {}
}
],
"rewrites": [
{
"source": "/api/:match*",
"destination": "https://myapidomain.com/:match*"
}
]
}
关于javascript - nuxt 上的 Axios api 代理不适用于现在 zeit 部署的服务器端渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57731357/
我创建了 4 个无服务器路由 /api/list (GET) /api/add (POST) /api/update/:id (PUT) /api/remove/:id(删除) 我将它们包含在 api
我有一个 next.js 服务器端呈现的应用程序。托管在 Zeit now 和 Firebase Hosting/Functions,同一个应用程序。Google insight给出不同的性能分析,
我在 now 托管了一个 vue.js 应用程序。它是一个已经编译的构建,仅使用 now --public 托管,没有 json 构建和其他内容。我想知道是否有可能在此部署中添加文件/ Assets
我有几个应用程序服务器运行多个节点应用程序(通过 PM2)。 我有一台 NGINX 服务器,它具有域的 SSL 证书和节点应用程序的反向代理。 在 NGINX 配置文件中,我设置了域及其位置 bloc
我收到以下错误:no such as file or directory public/uploads/bae1774e-d6dc-454b-ba63-a4c8c53d3053.png 当我将图像上传
我有一个 API 端点来上传文件,并且当前已通过 now 设置该端点。但是,文件上传后,我需要启动一个处理音频的进程,这可能需要一些时间(可能长达 5 分钟)。我将如何在 now/micro 框架内构
如何从 @zeit/next-sass 迁移和更改 next.config.js 文件以使用 Next.js 对 Sass 的内置支持? https://www.npmjs.com/package/@
我正在尝试在 zeit 托管上制作一个项目。我在 Express 中使用 Node,在 mysql2 中使用 Sequelize。 当我尝试在 zeit 中运行该应用程序时,它给了我以下错误: ---
在执行 PATCH 时,我无法正确处理 CORS 问题/POST/PUT来自浏览器的请求发送 Authorization带有 Bearer token 的标题(这在浏览器之外和 GET 请求中正常工作
我正在为一小段代码挠头,它永远不会解决 promise。它甚至没有告诉我 promise 可能被拒绝了。 const https = require('https'), { j
根据 the Zeit docs There are no limitations inside Docker deployments when it comes to the file system
我制作了一个部署在 now.sh 上的 Twitter 机器人 机器人正常工作了几个小时,然后就卡住了! 预计它会像以前一样发布每日趋势(Everyday)。之前,它还在新关注者之后进行直播,向他们发
在开发中,我可以直接链接到任何路由。在 Zeit/now 上,我可以将链接指向某些页面,但不能指向其他页面。我正在努力确定问题所在。 这是一个示例,直接导航到“https://partyshoegam
这是我的第一个问题,所以首先打个招呼,如果我的问题不符合所有标准或看起来很尴尬,请原谅。 我想知道我的应用程序是如何与 zeit 的 pkg 捆绑和编译的。 bundle 是否插入到预先确定大小的二进
我无法使用 now 命令将 Next.js 应用程序部署到 Zeit。 我尝试卸载sass并重新安装npm i node-sass以及npm i node-sass --force。这些都不起作用。我
HTTP GET 调用在本地运行良好,但在将应用程序部署到 heroku 或 zeit 时失败。 代码如下: const searchUrl = "https:/hn.algolia.com/api/
我正在使用 Zeit/Now 部署 Nuxt 应用程序。在开发阶段,我使用了 .env文件将 secret 存储到我的 Contentful CMS,将 secret 公开给 process.env使
在 Axios NuxtJS config 之后,我在我的 nuxt.config.js 上创建了这样的代理配置: proxy: { '/api/': { target: 'h
我最近从 now v1 升级至v2 . now dev 在本地运行一切并且所有页面都重新加载没有任何问题。但是,当我使用 now --prod 插入产品时并导航到一个页面,一切都按预期工作,但是如果我
我想连接到我的数据库 sqlite3。这在本地主机上运行良好,但在我部署到 zeit.co 时失败,出现错误: Error: SQLITE_CANTOPEN: unable to open datab
我是一名优秀的程序员,十分优秀!