如何将vuejs项目部署到heroku

hid*_*dar 8 heroku node.js vue.js vuejs2

我正在本地项目https://github.com/misterGF/CoPilot上工作,但现在它已经完成,我想把它推到heroku.问题是告诉heroku启动应用程序.

在heroku示例中,演示服务器在获取Procfile包含此数据的信息后运行

web: node index.js
Run Code Online (Sandbox Code Playgroud)

但在我的vuejs项目中,没有为服务内容的index.js.

我只有主入口点,index.htmlproc文件不能与HTML一起使用.

kan*_*ano 8

您还需要定义一个小型静态服务器来为您的index.html文件提供服务.一个简单的示例设置将http-server:

npm install -S http-server
Run Code Online (Sandbox Code Playgroud)

Procfile变成了:

web: http-server ./ -p $PORT
Run Code Online (Sandbox Code Playgroud)


bha*_*nsa 3

您需要更新您的 package.json 文件以使 heroku 了解这些软件包,例如,如果您使用express来提供文件:

"scripts": {
   "postinstall": "npm install express"
 }
 // "start": "node server.js" (executing server.js) during start
Run Code Online (Sandbox Code Playgroud)

服务器.js

var express = require('express')
var path = require('path')
var serveStatic = require('serve-static')

var app = express()
app.use(serveStatic(path.join(__dirname, 'dist')))

var port = process.env.PORT || 8000
app.listen(port)
console.log('server started ' + port)
Run Code Online (Sandbox Code Playgroud)

不要忘记构建.

请参阅此处以了解有关构建配置的更多信息:自定义构建过程 - Heroku


如果可以的话,您还可以像这样全局安装 http-server:

npm install -g http-server
Run Code Online (Sandbox Code Playgroud)

并更新 Procfile 以添加上述答案中提到的配置。

想知道:

  • npm install将安装“依赖项”和“devDependency”
  • npm install --生产环境只会安装“依赖项”
  • npm install -- dev将仅安装“devDependency”