简体   繁体   English

如何从 vercel 中的 static html 文件调用 expressjs 端点

[英]how to call expressjs endpoint from static html file in vercel

i have a very simple app with 1 /api/index.js server and 1 index.html file at the root.我有一个非常简单的应用程序,根目录有 1 个/api/index.js服务器和 1 个index.html文件。

index.js has a route app.get("/api/mystuff", () => {...}) index.js有一条路线app.get("/api/mystuff", () => {...})

index.html calls pings this route from a <script> with: index.html<script>调用 ping 这条路由:

const result = await fetch("/api/mystuff")

all of this works locally, but when deployed to Vercel i get hit with a 404 from my request.所有这些都在本地工作,但是当部署到 Vercel 时,我收到了 404 请求。 the endpoint it's hitting is https://myvercelapp.vercel.app/api/mystuff and i'm getting a The page could not be found NOT_FOUND error.它到达的端点是https://myvercelapp.vercel.app/api/mystuff并且我收到了The page could not be found NOT_FOUND错误。 I don't know how to get this working, can someone steer me in the right direction?我不知道如何让这个工作,有人可以引导我朝着正确的方向前进吗?

thanks!谢谢!

Based on the used tags I'm understanding you are using express with node.js.根据使用的标签,我了解到您正在使用 node.js 的快递。

Without seeing any of your code, I am guessing either your vercel.json isn't setup correctly (the guide explains this) or your index.js isn't setup correctly.没有看到您的任何代码,我猜测您的 vercel.json 设置不正确(指南对此进行了解释)或您的 index.js 设置不正确。


Vercel Guide on using Express Vercel 使用 Express 的指南

In that case, Vercel has a great guide on Using Express.js with Vercel .在那种情况下,Vercel 有一个关于使用 Express.js 和 Vercel的很好的指南。

In vercel's guide they have a section addressing using Standalone Express .在 Vercel 的指南中,他们有一个部分使用Standalone Express进行寻址。

In this guide they use an index.js inside of an api folder.在本指南中,他们使用api文件夹index.js

The index.js file: index.js 文件:

const app = require('express')();
const { v4 } = require('uuid');

app.get('/api', (req, res) => {
  const path = `/api/item/${v4()}`;
  res.setHeader('Content-Type', 'text/html');
  res.setHeader('Cache-Control', 's-max-age=1, stale-while-revalidate');
  res.end(`Hello! Go to item: <a href="${path}">${path}</a>`);
});

app.get('/api/item/:slug', (req, res) => {
  const { slug } = req.params;
  res.end(`Item: ${slug}`);
});

module.exports = app;

and the vercel.json which is what makes the whole project work.和 vercel.json 是使整个项目工作的原因。

{
  "rewrites": [{ "source": "/api/(.*)", "destination": "/api" }]
}

And finally, Adding a Public Directory which may explain more on how you can properly use Vercel with Express.最后, Adding a Public Directory可以解释更多关于如何正确使用 Vercel 和 Express 的信息。


The Problem问题

The reason I assume that it's the vercel.json is the problem is due it working locally and not on Vercel.我认为它是vercel.json的原因是问题是它在本地工作而不是在 Vercel 上工作。 A good way to test this locally is using vercel dev .在本地进行测试的一个好方法是使用vercel dev


Working Example工作示例

I've created a public example which may help you.我创建了一个可能对您有帮助的公开示例。 Please check https://github.com/Crispy-Cream/vercel-with-express for the source code example源代码示例请查看https://github.com/Crispy-Cream/vercel-with-express

and the public website https://vercel-with-express.vercel.app/api和公共网站https://vercel-with-express.vercel.app/api

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM