下面有几种办法:
假设你已经安装了全局的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