如何使用Zeit Now Server服务NodeJS Form应用程序

我正在zeit now服务器上运行一个简单的nodejs表单处理应用程序,并使用现在的CLI脚本进行部署。 这个Node应用程式虽然可以在上述连接埠号码(例如:http://localhost:8081/myaction)的localhost上运作良好,但是在zeit伺服器上,我无法使用相同的功能,因为我将localhost替换为NOW所提供的伺服器网址构建脚本。

这是我的now.json,我还提供对git存储库的访问,该存储库由节点服务器应用thaat处理该表单,发送电子邮件并在最后一步提供“感谢”页面。

节点服务器应用回购:https://github.com/jnsrikanth/node-form-server2

now.json文件内容:

{
  "version": 2,"builds": [{ "src": "index.js","use": "@now/node-server" }],"routes": [
    {
      "src": "/public/thank-you/dist/","dest": "/index.html","methods": ["GET"]
    },{
      "src": "/","dest": "/index.js","methods": ["POST"]
    }
  ]
}

package.json和其余代码可在存储库中找到。如果能在这里找到一些指针,那就太好了。

还请注意,在我的表单中,我正在调用action =“ https:// zeit-url:8081 / myaction” method =“ POST”,其中8081是Express应用程序正在侦听以处理POST方法的端口带有表单数据。

niksosf 回答:如何使用Zeit Now Server服务NodeJS Form应用程序

在您的 now.json 中,将/替换为/myaction

...
{
   "src": "/myaction","dest": "/index.js","methods": ["POST"]
}
...

只需拨打https://zeit-url/myaction(不带端口号)

希望对您有帮助

,

我建议您将该 now.json 转换为 vercel.json,并根据您提供的 github 存储库进行更新,如下所示:

"version": 2,"builds": [
    {
      "src": "index.js","use": "@vercel/node"
    }
  ],"routes": [
    {
      "src": "/public/thank-you/dist/","dest": "/public/index.html","methods": ["GET"]
    },{
      "src": "/","methods": ["POST"]
    }
  ]
  • 您需要验证此服务器在本地机器上运行没有问题。
  • 由于您使用的是 builds vercel.json 选项,因此您需要确保所有依赖项,如 node_modules 等都上传到 vercel,方法是从 {{1} },或者使用一些管道或 CI 操作。
本文链接:https://www.f2er.com/2612848.html

大家都在问