• 本地node服务启动vue打包项目


    下面有几种办法:
    假设你已经安装了全局的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('服务器开启成功');
    View Code

    2、打开命令窗口,cd到项目目录下,运行node server.js,控制台会输出“服务器开启成功”

    3、在浏览器中输入“localhost:8081/”+你要访问的文件名称;例如localhost:8081/index.html

  • 相关阅读:
    Vue-CLI
    Vue生命周期函数
    构建之法阅读笔记之四
    大二下个人总结
    个人加分项
    对老师的建议
    学习进度条 第九十一-第一百零五天 vue+uniapp app开发学习笔记
    第15周作业
    二进制安装mysql 5.7.31 启动报错/etc/init.d/mysqld: line 239: my_print_defaults: command not found
    获取最小数字
  • 原文地址:https://www.cnblogs.com/yeduweichengzhaoyu/p/13043110.html
Copyright © 2020-2023  润新知