下面有幾種辦法:
假設你已經安裝了全局的node.js。
方法1:
npm install -g http-server
在站點目錄下開啟命令行輸入http-server,運行結果如下:
在瀏覽器中訪問:http://localhost:8081/index.html就可以啦
方法2:
1.可以使用如下server.js放到vue項目的根目錄下

var http = require('http'); var fs = require('fs');//引入文件讀取模塊 var documentRoot = 'E:/my-study/my-project/dist'; //需要訪問的文件的存放目錄(項目所在位置的文件夾路徑) var server= http.createServer(function(req,res){ var url = req.url; //客戶端輸入的url,例如如果輸入localhost:8888/index.html //那么這里的url == /index.html var file = documentRoot + url; console.log(url); //E:/PhpProject/html5/websocket/www/index.html fs.readFile( file , function(err,data){ /* 一參為文件路徑 二參為回調函數 回調函數的一參為讀取錯誤返回的信息,返回空就沒有錯誤 二參為讀取成功返回的文本內容 */ if(err){ res.writeHeader(404,{ 'content-type' : 'text/html;charset="utf-8"' }); res.write('<h1>404錯誤</h1><p>你要找的頁面不存在</p>'); res.end(); }else{ res.writeHeader(200,{ 'content-type' : 'text/html;charset="utf-8"' }); res.write(data);//將index.html顯示在客戶端 res.end(); } }); }).listen(8081); console.log('服務器開啟成功');
2、打開命令窗口,cd到項目目錄下,運行node server.js,控制台會輸出“服務器開啟成功”
3、在瀏覽器中輸入“localhost:8081/”+你要訪問的文件名稱;例如localhost:8081/index.html