zoukankan      html  css  js  c++  java
  • 本地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

  • 相关阅读:
    如何取消隐藏工作簿,使工作簿可见
    Android小知识总结
    Android内存泄露总结
    Ubuntu 升级VisualBox后无法启动 Kernel driver not installed (rc=-1908)
    Eclipse颜色主题插件:Eclipse Color Theme
    使用Json的注意事项
    android中正确导入第三方jar包
    设计模式--单例模式学习
    比较好的学习网址总结
    二叉树学习总结(Java实现)
  • 原文地址:https://www.cnblogs.com/yeduweichengzhaoyu/p/13043110.html
Copyright © 2011-2022 走看看