Article Directory
Vue.js Scaffolding
- First, use Vue's scaffolding to create a vue project, which I use in practice
Vue2
, so follow the steps below to create - Vue CLI can create new projects of Vue 2 or Vue 3. You can choose the version of Vue you want when creating a new project.
- Here are the steps to create a Vue 2 project using Vue CLI:
Install Vue CLI (if you haven't already):
npm install -g @vue/cli
- This will globally install the latest version of Vue CLI.
create new project
vue create my-project
-
Replace
my-project
with your desired project name. -
Choose
Vue 2
a preset: After runningvue create
the command, Vue CLI will let you choose a preset. You can choose the "Default (Vue 2)" preset to create a Vue 2 project. If you need a more complex configuration, such as including Vuex or Vue Router, you can select "Manually select features" and manually select Vue 2 and other options you need. -
Then it's time to develop your project
Heroku deploys Vue2
- This article is applicable to the situation: you use to
Vue
build the front-end, and another framework (Django) to build the back-end, and you want to deploy the front-end and back-end to two different Heroku projects (because the Django-based project Heroku deployment is very troublesome, so I will deploy separately), then you need to deploy itVue
separately Vue2
What is created is just a front-end content. In order to successfully deploy, we need toVue
add a file to this projectserver.js
. You can think of it asHeroku
an entity deployed on . Thisserver.js
will allow people who visit this application to get the front-end static page when it is running.- The following is an
server.js
example of
project directory
.
├── Procfile
├── README.md
├── babel.config.js
├── index.js
├── jsconfig.json
├── package-lock.json
├── package.json
├── public
│ ├── favicon.ico
│ └── index.html
├── server.js
├── src
│ ├── App.vue
│ ├── assets
│ ├── components
│ ├── http.js
│ ├── main.js
│ ├── router
│ ├── test.html
│ ├── test.js
│ └── views
└── vue.config.js
- Except
server.js
other is my original file directory server.js
Create a new file directly under the root directory
server.js
- Doing this step actually creates a static server, you can use
Node.js
theexpress
library to create a simple server for your Vue application - Install express:
npm install express --save
const express = require('express');
const serveStatic = require('serve-static');
const path = require('path');
const app = express();
app.use('/', serveStatic(path.join(__dirname, '/dist')));
const port = process.env.PORT || 8080;
app.listen(port);
console.log('Listening on port: ' + port);
- Because
Vue
the static resources of the project created by scaffolding are placed/dist
under the directory, although you can see it from my file directory,public
but don't think that this is where the static resources are stored, and don't writeapp.use('/', serveStatic(path.join(__dirname, '/dist')));
asapp.use('/', serveStatic(path.join(__dirname, 'public')));
package.json
- This file usually defines some
script
(scripts), that is, start your server or other behaviors with some short commands
"scripts": {
"start": "node server.js",
"build": "vue-cli-service build"
}
Create Procfile
Procfile
is an extension-less text file in which you define your application's process type and the command to run. This way Heroku knows how to run your app.Procfile
Create and add the following in the project root directory :
web: npm run start
Follow heroku's guide to initialize git and submit code
- Create a new heroku app
- Submit the code following
Deploy
the guide in
possible problem
-
If the Heroku deployment succeeds without displaying any pages, and there are no error messages, it could be due to several reasons. Here are some possible problems and corresponding solutions:
-
Check your build script: Make sure you
package.json
have the correct build command defined in the file, usuallybuild
:"vue-cli-service build"
. When you push to Heroku, Heroku will automatically run this build command to create your Vue.js app. -
Confirm server.js settings: Check that your
server.js
file path is set correctly for static files. If you created the project using Vue CLI, the generated static files will be locateddist
in the directory. You need to make sure the express.static middleware points to this directory. For example: -
Make sure the Procfile is set up correctly:
Procfile
the file that tells Heroku how to launch your application. Make sure the Procfile content is correct and the web process type points to the correct start command. For example, if you useserver.js
a file to start the server, itProcfile
should contain the following:web: node server.js
-
Check the Heroku logs: Even if Heroku doesn't show errors directly on the app page, there may be more information in its logs. Run
heroku logs --tail
the command to view the latest log information and newly generated logs.