vue-router history mode server-side configuration process record

vue-router history mode server-side configuration process record

history route

History mode refers to the mode of implementing client-side routing using HTML5's history API. Its typical performance is to remove the # in the URL path in hash mode. It is very easy to enable history mode when using Vue-Router. You only need to pass in the mode:'history' configuration item when instantiating the route. However, when there is no server support, the route based on the historyAPI cannot directly access the specified page from the URL address bar. This is easy to understand, because entering the URL address bar and pressing Enter is equivalent to sending a GET request, then the route path without # is the same as the ordinary API interface. Since the server does not define such an interface, it is normal to see a 404 page when accessing directly.

Official examples

The official website provides many ways to handle this scenario. Take the node.js version as an example:

const http = require('http')
const fs = require('fs')
const httpPort = 80

http.createServer((req, res) => {
  fs.readFile('index.htm', 'utf-8', (err, content) => {
    if (err) {
      console.log('We cannot open "index.htm" file.')
    }

    res.writeHead(200, {
      'Content-Type': 'text/html; charset=utf-8'
    })

    res.end(content)
  })
}).listen(httpPort, () => {
  console.log('Server listening on: http://localhost:%s', httpPort)
})

It is not difficult to see that its processing idea is to force all requests to be redirected to the home page, which is equivalent to the server blocking the situation where the access resource does not exist, and leaving the routing work to the client to handle. In this way, the front-end routing with history mode enabled will not report an error when directly locating the sub-page.

vue-router history mode configuration

The history mode of vue-router requires setting the mode in the routing configuration to history, and corresponding configuration is also required on the server side.

Compared with hash mode, in history mode, the URL of the page will have fewer symbols such as #:

hash: www.example.com/#/login

history: www.example.com/login

In a single-page application, in history mode, the browser will request this page from the server, but the server does not have this page and returns 404. So at this time you need to configure the server: in addition to static resources, you need to return the index.html of the single-page application.

Server configuration - nodejs

In the nodejs server, you need to introduce the connect-history-api-fallback module to handle the history mode, and use this module before using the middleware for processing static resources:

const path = require('path')
// Import the module that handles history mode const history = require('connect-history-api-fallback')
const express = require('express')

const app = express()

// Register the middleware for handling history mode app.use(history())
// Middleware for processing static resources app.use(express.static(path.join(__dirname, './web')))

app.listen(3000, () => {
    console.log('service started at port 3000')
})

Server Configuration - nginx

First put the packaged files into the html folder, then open the nginx.conf file in the conf folder and modify the following configuration:

http {
    # ...other config
    server {
        # ...other config
        location / {
            root html;
            index index.html index.htm;
            try_files $uri $uri/ /index.html;
        }
    }
}

Summarize

This is the end of this article about the server-side configuration of vue-router history mode. For more relevant vue-router history mode configuration content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • The difference between hash mode and history mode in Vue-router
  • The difference between hash and history modes in vue-router
  • Detailed explanation of the changes required to change the default hash mode of vue-Router to history
  • vue-router enables development and non-root directory deployment in history mode
  • Summary of WeChat sharing in vue-router history mode
  • HTML5 History mode settings for vue-router
  • A brief discussion on the pitfalls encountered in the history mode after the launch of vue project 4rs vue-router
  • An article to understand the hash mode and history mode of vue-router
  • Detailed explanation of the difference between hash mode and history mode in Vue-router
  • Distinguish between hash and history modes of vue-router

<<:  Detailed steps for installing and debugging MySQL database on CentOS7 [Example]

>>:  Summary of the differences between Mysql primary key and unique key

Recommend

Vue implements various ideas for detecting sensitive word filtering components

Table of contents Written in front Requirements A...

A brief discussion on how to use slots in Vue

How to define and use: Use the slot tag definitio...

How to ensure transaction characteristics of MySQL InnoDB?

Preface If someone asks you "What are the ch...

Solve the problem that Docker pulls MySQL image too slowly

After half an hour of trying to pull the MySQL im...

Two ways to open and close the mysql service

Method 1: Use cmd command First, open our DOS win...

Detailed explanation of scheduled tasks for ordinary users in Linux

Preface Ordinary users define crontab scheduled t...

Brief analysis of the MySQL character set causing database recovery errors

Importing data with incorrect MySQL character set...

Summary of CSS counter and content

The content property was introduced as early as C...

Specific method to delete mysql service

MySQL prompts the following error I went to "...

Graphic tutorial on installing tomcat8 on centos7.X Linux system

1. Create the tomcat installation path mkdir /usr...

6 ways to view the port numbers occupied by Linux processes

For Linux system administrators, it is crucial to...

Analysis of a MySQL deadlock scenario example

Preface Recently I encountered a deadlock problem...

Detailed explanation of vue page state persistence

Table of contents Code: Replenish: Summarize Requ...

JS uses canvas technology to imitate echarts bar chart

Canvas is a new tag in HTML5. You can use js to o...