LoginSignup
5
3

More than 3 years have passed since last update.

【Vue.js + express】本番環境のルーティングを設定する。

Posted at

前提

rootパスのルーティングは設定済。

server.js
const express = require('express');
const port = process.env.PORT || 8080;
const app = express();
app.use(express.static(__dirname + "/dist/"));
app.listen(port);

目的

本番環境のrootパス以外のルーティングを設定する。

エラー確認

$ node server.js し、http://localhost:8080/hoge にアクセス。

エラー内容

Cannot GET /hoge
_.png

解決

server.jsファイル内を変更

▼ 変更前

server.js
const express = require('express');
const port = process.env.PORT || 8080;
const app = express();
app.use(express.static(__dirname + "/dist/"));
app.listen(port);

▼ 変更後

server.js

const express = require("express");
const port = process.env.PORT || 8080;
const app = express();

app.use(express.static(__dirname + "/dist/"));
app.get(/.*/, function(req, res) {
  res.sendfile(__dirname + "/dist/index.html");
});
app.listen(port);

console.log("Server started...");

参考

https://www.youtube.com/watch?v=yfW9knTBR90
https://github.com/jordanhudgens/vue-cli-three-heroku-template/blob/master/server.js

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3