javascript - 是否可以在 Vercel 中部署 NodeJs 应用程序?

标签 javascript node.js

我正在尝试从 CLI 在 Vercel ( https://vercel.com ,现在之前)部署一个 API(在 Node 中制作)。但是当我部署应用程序时,我打开站点,结果只是路径目录中的文件,而不是正在运行的应用程序。这是我的server.js

    {
  "name": "subtitles-api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node server.js",
    "pre-deploy": "node deleteLastDeploy.js",
    "deploy": "npm run pre-deploy && now --public && now alias",
    "test": "echo \"Error: no test specified\" && exit 1"
  }
  "engines": {
    "node": ">=6.9"
  },
  "keywords": [],
  "author": "",
  "license": "MIT",
  "dependencies": {
    //list of dependencies
  }
}

要查看完整的 API:https://github.com/bitflix-official/subtitles-api

最佳答案

运行yarn global add now@latest安装 CLI

  • 创建 now.json归档并粘贴此
  • {
      "version": 2,
      "builds": [{
        "src": "./server.js",
        "use": "@now/node-server"
      }],
      "routes": [{"handle": "filesystem"},
        {
          "src": "/.*",
          "dest": "server.js"
        }
      ]
    }
    
    
    备注 : 更改 "src": "server.js", && "dest":"server.js"到您的服务器入口文件。
  • 添加到.gitignore
  • 然后运行 ​​now在 CLI 中进行部署。

  • 如果您要部署到生产环境,请使用 now --prod CLI 中的命令进行部署
    这是我部署的示例服务器:https://vercel-example-server.now.sh .

    关于javascript - 是否可以在 Vercel 中部署 NodeJs 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61808973/

    相关文章:

    javascript - jQuery $.getJSON 对每个控件只工作一次。不再到达服务器

    javascript - React Native - 警告 : Can't perform a React state update on an unmounted component

    css - 如何有效地使用 bootstrap-sass?

    node.js - Mongoose find 和 findOne 中间件不工作

    node.js - Nestjs 中的 Observables - 异步读取文件

    javascript - jQuery 未捕获类型错误 : Cannot read property 'nodeType' of undefined

    javascript - 从 Ajax 加载页面提交 Ajax 表单

    javascript - 在 JavaScript 中动态创建一个带有默认参数的函数

    javascript - 无法将值从 JQuery 日期范围选择器传输到输入字段

    node.js - 服务器发送的事件在简单快速 SSE 的 chrome devtools 中显示为空白