linux運行vue編譯后的項目方式
如果你的 Vue 項目使用了 history 模式(而非默認(rèn)的 hash 模式),在純靜態(tài)服務(wù)器中會出現(xiàn)類似的問題。因為 Vue Router 的 history 模式要求所有未匹配的路徑都重定向到 index.html,以便 Vue 前端處理路徑。
首先在本地執(zhí)行npm run build編譯項目,會生成一個dist的項目源碼文件
1.創(chuàng)建一個簡單的 HTTP 服務(wù)器
修改你的 server.js,確保所有未匹配的請求都返回 index.html。
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 14515;
http.createServer((req, res) => {
let filePath = path.join(__dirname, req.url === '/' ? '/index.html' : req.url);
fs.readFile(filePath, (err, data) => {
if (err) {
// 如果文件不存在,返回 index.html
fs.readFile(path.join(__dirname, 'index.html'), (err, indexData) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('500 Internal Server Error');
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(indexData);
}
});
} else {
// 返回找到的文件
const ext = path.extname(filePath).toLowerCase();
const mimeTypes = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
};
res.writeHead(200, { 'Content-Type': mimeTypes[ext] || 'application/octet-stream' });
res.end(data);
}
});
}).listen(PORT, () => {
console.log(`Server running at http://0.0.0.0:${PORT}/`);
});
2.運行
在 dist 目錄下啟動服務(wù)器:
node server.js nohup node server.js & //在后臺可以運行
項目目錄

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Apache NameVirtualHost *:80 has no VirtualHosts問題解決辦法
這篇文章主要介紹了Apache NameVirtualHost *:80 has no VirtualHosts問題解決辦法,一個很簡單的配置性錯誤,需要的朋友可以參考下2014-08-08
Linux下使用SSH遠(yuǎn)程執(zhí)行命令方法收集
這篇文章主要介紹了Linux下使用SSH遠(yuǎn)程執(zhí)行命令方法收集,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10
Centos7 利用LVM實現(xiàn)動態(tài)擴容的方法
本篇文章主要介紹了Centos 7 利用LVM實現(xiàn)動態(tài)擴容的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
Linux 系統(tǒng)雙網(wǎng)卡綁定配置實現(xiàn)
這篇文章主要介紹了Linux 系統(tǒng)雙網(wǎng)卡綁定配置實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
使用FileZilla從Linux系統(tǒng)下載文件的方法
最近做項目,遇到這樣的需求,要求將Linux系統(tǒng)的的某個文件夾下載到我Windows系統(tǒng)某個文件夾里,怎么實現(xiàn)這個功能呢?下面腳本之家小編給大家?guī)砹耸褂肍ileZilla從Linux系統(tǒng)下載文件的方法,感興趣的朋友一起看看吧2018-07-07

